473,399 Members | 2,478 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,399 software developers and data experts.

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_file('xml/prices.xml');
....and for the xml document will be something like:

<?xml...
<items>
<item id="subscription">
<signup>40.00</signup>
<monthly>15.00</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
Jul 17 '05 #1
1 2156
"mr_burns" <bi******@yahoo.co.uk> wrote in message
news:65**************************@posting.google.c om...
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_file('xml/prices.xml');
...and for the xml document will be something like:

<?xml...
<items>
<item id="subscription">
<signup>40.00</signup>
<monthly>15.00</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


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


Jul 17 '05 #2

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

Similar topics

4
by: Kevin | last post by:
I have a document, lets say: <Doc> <Ele1 Att1='hello'> <Ele2 Att2='goodbye'/> </Ele1> </Doc> In XSLT I need to generate an attribute value which contains part of this document as a text...
3
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the following: Some text<br/> here with</br> lots...
7
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the...
1
by: Eric | last post by:
I am trying to figure out a good way to implement a XSLT transformation. Basically my goal is that I want to be able to ouput the following XML in a document: <chart type="pie" width="100"...
3
by: Andy Fish | last post by:
Hi, I just found this template in someone else's xslt (it's Microsoft's "word2html" stylesheet to convert WordProcessingML to HTML) <xsl:template match="WX:sect"> <xsl:variable...
6
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET...
1
by: David | last post by:
All I am using the code below in order to read an XML file in the format: <root> <selection-attr attr="p70"> <value code="Mr">Mr</value> <value code="Mrs">Mrs</value> <value...
2
by: =?Utf-8?B?S2V2aW4=?= | last post by:
I have an XML node that has both attributes and text. I am having difficulty getting the xmlreader to deal with this. Sample XML Node: <HomePhoneNumber Attribute1="Y" Attribute2="Y"...
0
by: Gregg | last post by:
Hello; I have the below XML schema which I'm trying to iterate through node by node. The script I'm using is also shown below. The resulting output, also shown below, shows that I'm not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.