Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML children 1

Status
Not open for further replies.

porta325

Programmer
Jul 13, 2008
1
RO
Hello, i have a problem witch i think it's verry easy for someone who knows some XML.
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

<?php
$xml = simplexml_load_file("test.xml");foreach ($xml->children() as $child)
{
echo "Child node: " . $child . "<br />";
}
?>

This outputs perfect.
But what i want to do is this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<test>
<note1>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note1>
<note2>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note2>
</test>
From the root test i want to output only <test1> or <test2>.
Can anyone please help ?
 
[1] For better approach, you shouldn't keep fabricate new tag. I mean you should keep "note1" and "note2" the same name "note". Let the position of it or any other characteristic to distinguish one from another.

[2] I assume thereby note at the place of note1 and note2. The search is through a well-conceived xpath.
[tt]
$xml = simplexml_load_file("test.xml");

$result=$xml->xpath("/test/note");
//if you need the first, it is this:
//$result=$xml->xpath("/test/note[1]");
//if you need the second it is this:
//$result=$xml->xpath("/test/note[2]");
//etc etc

while(list( , $node) = each($result)) {
foreach ($node->children() as $child){
echo "Child node: " . $child . "<br />";
}
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top