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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    #1

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

    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:

    <?php

    $doc = domxml_open_fil e('xml/prices.xml');


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

    <?xml...
    <items>
    <item id="subscriptio n">
    <signup>40.00 </signup>
    <monthly>15.0 0</monthly>
    </item>
    ..
    ..
    ..etc


    ....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,
    cheers

    burnsy
  • CJ Llewellyn

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

    "mr_burns" <bissatch@yahoo .co.uk> wrote in message
    news:651c6ea9.0 408221348.21db8 c64@posting.goo gle.com...[color=blue]
    > 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:
    >
    > <?php
    >
    > $doc = domxml_open_fil e('xml/prices.xml');
    >
    >
    > ...and for the xml document will be something like:
    >
    > <?xml...
    > <items>
    > <item id="subscriptio n">
    > <signup>40.00 </signup>
    > <monthly>15.0 0</monthly>
    > </item>
    > .
    > .
    > .etc
    >
    >
    > ...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,
    > cheers[/color]

    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
    object/class.

    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)
    {
    if($cnItems->has_child_node s())
    {
    foreach($cnItem s->child_nodes( ) as $cnSubItems)
    {
    echo $cnSubItems->node_name();
    }
    }
    }




    Comment

    Working...