Hindi Songs

Search About PHP!!!

Thursday, July 9, 2009

Using SimpleXML

Below is an XML file:

< ?xml version="1.0" encoding="ISO-8859-1"? >
<>
<>Tove< /to >
<>Jani< /from >
<>Reminder< /heading >
<>Don't forget me this weekend!< /body >
< /note >

We want to output the element names and data from the XML file above.

Here's what to do:

1. Load the XML file

2. Get the name of the first element

3. Create a loop that will trigger on each child node, using the children() function

4. Output the element name and data for each child node

Example

< ?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "
";

foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "
";
}
? >

The output of the code above will be:

note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!