473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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>
Jul 17 '05 #1
1 2559
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
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
6
by: Scott Simpson | last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly XPath book and I'm using XPath Visualizer on Windows and that seems to work fine, but I'm looking for something on Linux....
2
by: AR | last post by:
Hi, How can I get strings separately which contain text nodes of a given element using XSL and XPath? For example, <a> aaa 111 <b>
1
by: alpha | last post by:
Hi Friends, When we need the xpath namespace as we can traverse the xml document using xmldocument object like using selectSingleNode or selectNodes methods. In system.xml.xpath namespace we have to...
3
by: baggy | last post by:
Hi, I have the following XML:- <X1> <Props> <Prop xsi:type=\"text\" Name=\"N1\"><Value>TestUser</Value></Prop> <Prop xsi:type=\"bool\" Name=\"flag\"><Value>0</Value></Prop> <Prop...
6
by: Armel Asselin | last post by:
Hello, I'm searching for a simple command line tool to manipulate XML files. The idea would be commands such as that: xmanip-tool set /document/xpath/@name="value" remove //wrong-nodes add...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
2
by: =?Utf-8?B?Z2lkZHk=?= | last post by:
hi, !!Please help! I've been at this for a few hours now! I'm probably doing something silly. heres my xml document: <Log> <entry user="Gideon" date="11/6/2008" time="10:14 AM"...
5
Plater
by: Plater | last post by:
I'm having a bit of trouble getting XPath expressions to work to select nodes. I am working with the google maps results. A shortened example xml: <?xml version="1.0" encoding="UTF-8" ?> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.