XPath and PHP

Discussion in 'PHP' started by Omzy, Jun 2, 2009.

  1. #1
    How can I use XPath to read in an XML document, run a query to filter out some of the data and then save the filtered data as a new XML file?
     
    Omzy, Jun 2, 2009 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    It would be a huge help if you posted an example of what the XML document should look like before and after processing.
     
    joebert, Jun 2, 2009 IP
  3. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Just a standard XML document really.
     
    Omzy, Jun 2, 2009 IP
  4. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You'll probably find it easier to use simpleXML, but the basic method for parsing with XPath is:

    $xml = file_get_contents('yourxml.xml');
    $dom = new DOMDocument();
    @$dom->loadXML($xml);
    $xpath = new DOMXPath($dom);
    $xml_nodes = $xpath->evaluate("/your/xpath/goes//here");
    foreach ($xml_nodes as $nodes) {
      echo $nodes->firstChild->data;
    }
    PHP:
     
    JDevereux, Jun 2, 2009 IP
  5. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK I've already got that far, I need it to run a query to filter out some of the data and then save the filtered data as a new XML file.
     
    Omzy, Jun 2, 2009 IP
  6. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    use fwrite()
     
    JDevereux, Jun 2, 2009 IP