tags:

views:

38

answers:

2
+1  Q: 

PHP Xpath help.

Hi,

Can anyone point me to a good tutorial on xpath or give me a quick lesson here? Also if xpath is for querying xml, what would I need to use to modify the xml? I guess it would need to be something that works hand in hand with xpath?

Cheers, Franky

A: 

There's no "quick" introduction to XPath unless you give some example queries. I found the reference/tutorial from w3schools to be quite good.

To modify XML documents in PHP you can use SimpleXML or XMLReader/XMLWriter (and probably haps of other XML implementations in PECL)

halfdan
A: 

Here is a good overview of working with XML in PHP.

Instead of using XPath, I would recommend using SimpleXML and DOM to parse it. For example...

<?xml version="1.0" encoding="UTF-8"?>
<books> 
    <book> 
       <title>Great American Novel</title> 
       <characters> 
        <character> 
         <name>Cliff</name> 
         <desc>really great guy</desc> 
        </character> 
    </characters>
  </book>
</books>

You could parse by doing this (assuming XML is in $xmlstr)...

<?php 

  $xml = new SimpleXMLElement($xmlstr); 

  echo $xml->book[0]->title; // "Great American Novel" 
  ?> 
Ryan Berger
Cheers, that is exactly what I was looking for.
Franky Chanyau
You don't do any xpath in your example.
halfdan
That's true, my example uses DOM instead. I edited my answer to reflect this.
Ryan Berger