tags:

views:

58

answers:

2

im using file_get_conents to retrieve an XML file.

Lets say the xml file looks like this

<tag1>
      <subtag1>Info</subtag2>
</tag1>

How would i go about retrieving Info from that xml file?

+1  A: 

Easiest thing to do is to use SimpleXML: http://us.php.net/manual/en/simplexml.examples-basic.php

Basicall, you can do something like:

$xmlString = file_get_contents('foo.xml');
$xml = new SimpleXMLElement($xmlString);

or, even easier:

$xml = simplexml_load_file('foo.xml');

Check out the docs linked above, should have all you need :)

Ian Selby
+1  A: 

You can use SimpleXML to retrieve your information as such:

$xml = simplexml_load_file('file.xml');

$value = $xml->tag1->subtag1;
echo $value; // Will Output "Info"

If you want to loop through subtags:

// Method One
foreach($xml->tag1->children() as $subtag) {
    echo $subtag . "\n";
}

// Method Two
$i = 1;
while(($subtag = $xml->tag1->{"subtag".$i}) !== null) {
    echo $subtag . "\n";
    $i++;
}
Andrew Moore