Locating a node's text value by tag name or attribute

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi there,

I dont normally use DOM XML but have been given the task of
integrating it with a web site using PHP. I have to use DOM XML as
this is what is provided by the server.

Basically what I am tryin to do is get it to load an XML document
(that I can do) and find a specific node by its id attribute. The code
I am up to is:


$doc = domxml_open_file('xml/prices.xml');

....and for the xml document will be something like:

  <item id="subscription">

....please dont be too blown away by my progress :-), I have no idea
how to simply from there locate an item with a specific id attribute
(ie. subscription) and then firstly get the text for <signup> and then
<monthly>. Also, some items with other id attributes many have just
<price> nodes instead of <signup> and <monthly> so I will need to
identify these nodes by their tag name.

I apologies as I know that there are tutorials available but they all
seem to get a bit heavy and not really simple methods. I have had
trouble finding a tutorial page that basically tells you how to locate
a node by attribute or name. Any help would be much appreciated,


Re: Locating a node's text value by tag name or attribute

Quoted text here. Click to load it

When you open a xml file you create a complex data structure consisting of
objects and arrays of objects, starting at the top level with a domDocument

That document will have an array of childNode objects/class.

To read through the data in your document you need to read through each
child node.

foreach($doc->child_nodes() AS $cnItems)


You can then see what type of child it is, or what tag it represents

foreach($doc->child_nodes() AS $cnItems)
    echo $cnItems->node_type();
    echo $chItems->node_name();

Again, if the node is a parent node then you can use the foreach structure
on it

foreach($doc->child_nodes() AS $cnItems)
        foreach($cnItems->child_nodes() as $cnSubItems)
            echo $cnSubItems->node_name();

Site Timeline