Stenan
04-15-2008, 04:56 AM
I have an xml file named test_forum.xml, with the following structure:
<?xml version="1.0" standalone="yes"?>
<tests>
<test>
<title>Test 1</title>
<number>15</number>
<client>Client 1</client>
</test>
<test>
<title>Test 2</title>
<number>16</number>
<client>Client 2</client>
</test>
</tests>
In PHP 5.2, I use SimpleXML to load the file in the variable $xml as following:
$path_xml = 'test_forum.xml';
$xml = simplexml_load_file($path_xml);
To print the tags <title>, I use xpath as following:
foreach ($xml->xpath('//test') as $t) {
echo $t->title, '<BR>';
}
The output is as expected:
Test 1
Test 2
Now the problem: using foreach I'd like to print, together with the tag <title>, also the relative position inside of the array, something like this:
0 - Test 1
1 - Test 2
where 0 and 1 are the positions of the tags inside of the original xml, where Test 1 is the node title in test[0], and Test 2 is the node title in test[1].
How to do it? I'd be gratefull to anyone could help me find a solution.
Thanks
Stefano
<?xml version="1.0" standalone="yes"?>
<tests>
<test>
<title>Test 1</title>
<number>15</number>
<client>Client 1</client>
</test>
<test>
<title>Test 2</title>
<number>16</number>
<client>Client 2</client>
</test>
</tests>
In PHP 5.2, I use SimpleXML to load the file in the variable $xml as following:
$path_xml = 'test_forum.xml';
$xml = simplexml_load_file($path_xml);
To print the tags <title>, I use xpath as following:
foreach ($xml->xpath('//test') as $t) {
echo $t->title, '<BR>';
}
The output is as expected:
Test 1
Test 2
Now the problem: using foreach I'd like to print, together with the tag <title>, also the relative position inside of the array, something like this:
0 - Test 1
1 - Test 2
where 0 and 1 are the positions of the tags inside of the original xml, where Test 1 is the node title in test[0], and Test 2 is the node title in test[1].
How to do it? I'd be gratefull to anyone could help me find a solution.
Thanks
Stefano