Struggling with simple DOM/XPath usage...

This is a discussion on Struggling with simple DOM/XPath usage... within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi All, I have a task that should be very simple but I'm running into trouble. All I want ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-04-2005
Joshua Beall
 
Posts: n/a
Default Struggling with simple DOM/XPath usage...

Hi All,

I have a task that should be very simple but I'm running into trouble. All
I want to do is query a document using XPath, and save the resulting XML in
a string. Here's that I am trying (PHP5):

// $xml has the entire XML document in it; I included it for reference at
the end of the document
$dom = new DOMDocument();
$dom->loadXML($xml);

$xpath = new DOMXPath($dom);
$query = "//xml/page[attribute::name='other']";
$result = $xpath->query($query);

foreach ($result as $node)
{
//echo $node->saveXML(); // Bombs with an error, as saveXML is not a
method of $node
$page = new DOMDocument();
$page->importNode($node);
echo $page->saveXML(); // The XML document it prints is nothing but the
xml declaration (<?xml ... ?>)
$s = simplexml_import_dom($node);
echo $s->asXML(); // This works - I get a valid piece of XML back,
which tells me that my XPath query must be working (right?)
}

I'd rather do everything using DOM at this point, since SimpleXML is still
in the experimental stage. I'd also like to know the DOM way of doing this.
Can someone clue me in? What am I doing wrong?


**** XML document:
<?xml version="1.0" encoding="iso-8859-1" ?>
<xml>
<page name='landing'>
<dummyTag>Just for testing</dummyTag>
<owner>John Brown</owner>
</page>
<page name='other'>
<dummyTag>More testing tags</dummyTag>
<owner>John Smith</owner>
</page>
</xml>


Reply With Quote
  #2 (permalink)  
Old 02-04-2005
Joshua Beall
 
Posts: n/a
Default Re: Struggling with simple DOM/XPath usage...

I think I got it... looks like what I have to do is replace the line that
was $page->importNode($node) with::

$importedNode = $page->importNode($node,true);
$page->appendChild($importedNode);

Then I can save the XML to a string by doing:

$resultXML = $page->saveXML();

This seems a bit "long-winded" though... is this the proper way to do what I
am trying to accomplish?

Also, apologies for not setting FUT. It should be set to comp.lang.php now.

-jb


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:18 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0