473,623 Members | 3,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($no de);
echo $page->saveXML(); // The XML document it prints is nothing but the
xml declaration (<?xml ... ?>)
$s = simplexml_impor t_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>Jus t for testing</dummyTag>
<owner>John Brown</owner>
</page>
<page name='other'>
<dummyTag>Mor e testing tags</dummyTag>
<owner>John Smith</owner>
</page>
</xml>
Jul 17 '05 #1
1 2577
I think I got it... looks like what I have to do is replace the line that
was $page->importNode($no de) with::

$importedNode = $page->importNode($no de,true);
$page->appendChild($i mportedNode);

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
1917
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
8296
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. I've tried Quanta. It seems to be able to run the xsltproc processor to do XSLT transformations but it doesn't seem to cope with interactively typing in XPath expressions. (As a side note, what other tools are useful with XML on Linux and...
2
1551
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
1761
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 use XPathNavigator , XPathExpression , XPathNodeIterator class to get the same result what we get it thru selectNodes ....and that too iterator can be traversed in forward direction only. Please tell me the usage of system.xml.xpath other than...
3
1264
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 xsi:type=\"int\" Name=\"numemp\"><Value>100</Value></Prop> </Props> </X1>
6
6814
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 /document/node=\<hello\ < in.xml out.xml so, it would simple add/remove/change commands (either as arguments or input script file), a bit of xpath and the usual Unix parameters (-o output,-f
3
4967
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 version="1.0" encoding="utf-8" ?> <thing xmlns="urn:thing-schema-v1"> <foo>foo thing</foo> <bar>bar thing</bar>
2
6296
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" action="Searched" /> <entry user="Jennie" date="11/6/2008" time="10:14 AM" action="Finished her
5
4299
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" ?> <kml xmlns="http://earth.google.com/kml/2.0"> <Document> <Placemark>
0
8162
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8662
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8603
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5560
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.