View Full Version : php xml parser

10-13-2009, 11:58 PM
Hi People,

I'm trying to parse a xml file which has the following structure using simplexml_load_file:

<title>Re: It&apos;s Jerrytime: The Strange Trip</title>
<description>Video Cam Direct Upload</description>

<comment>he roles with a peice of **** krew
he dosent know ****
sdk is the one of the best graff krews around vancouver,
aswell as lca, rts, bys, ydk, and tks</comment>
<commentDate>2008-04-16 17:22:14.0 BRT</commentDate>
<comment>I love his style. Its so simple and looks awesome.</comment>
<commentDate>2008-02-13 13:08:59.0 BRST</commentDate>
<commentDate>2008-02-12 18:32:10.0 BRST</commentDate>

I'm using the following code:


$xml = simplexml_load_file("amostra/$v.APIDATA.xml");

$titulo = $xml->xpath('/ytvideo/title'); ok
$idArquivo = $xml->xpath('/ytvideo/integerid'); ok
$description = $xml->xpath('/ytvideo/description'); ok
$category = $xml->xpath('/ytvideo/category'); ok

$comments = $xml->xpath('comments/ytcomment');


echo "<br>titulo: $titulo[0]<br>"; ok
echo "idArquivo: $idArquivo[0]<br>"; ok
echo "category: $category[0]<br>"; ok
echo "description: $description[0]"; ok

However I'm in doubt about how to parse what I've got by
$comments = $xml->xpath('comments/ytcomment');

The print_r($comments) returns
Array ( [0] => SimpleXMLElement Object ( [id] => 95F6B44F4D77F00E [integerid] => -772199745 [author] => xFalcon417x [comment] => oh nos! you found outs :o! :) [commentDate] => 2008-02-25 20:00:09.0 BRT ) [1] => SimpleXMLElement Object ( [id] => 2764CBA934E1BD2A [integerid] => -846351560 [author] => xFalcon417x [comment] => hahaha yeah winter is pretty beat if you ask me... unless there is madddd snow lol. [commentDate] => 2008-06-06 21:35:18.0 BRT )

Is there any chance for me to transforme $comments into some kind of array like that