|
Posted by Malcolm Dew-Jones on 10/16/35 11:18
Stefano (texstefano@libero.it) wrote:
: I would like to get the value of a tag in an xml file, the problem is
: that, his structure is not <item>value</item> but is:
: <Cube>
: - <Cube time="somevalue">
: <Cube rate="the value i want to get" />
: <Cube rate="the value i want to get" />
: <Cube rate="the value i want to get" />
: and so on.....
: So, i have two questions, how do i get the values of the 3rd "<Cube>"
: when it is inside the tags and not ouside? and how do I choose the
: right one, having three of them?
In php 4, I use the xml parser that comes with php. The xml parser is
going to call a function that you have to write, and which you have to
register before the parsing starts.
e.g.
$xml_parser = xml_parser_create();
xml_set_element_handler( $xml_parser, "startElement", "endElement");
You need to provide functions called startElement and endElement. The last
question is how to get the third tag. To do that you need to count the
tags as you find them. The simplist way is to use a global variable.
So, your startElement function could look something like the following at
the top
$count=0;
function startElement($parser, $name, $attrs)
{
global $count;
if ($name == 'Cube')
{
$count++;
$the_rate = $attrs['rate'];
if ($count==3)
{
die("all done, value = $the_rate\n");
(Of course you probably wouldn't use die, but it illustrates the point.)
--
This space not for rent.
[Back to original message]
|