Reputation: 66549
I am trying to parse an xml string $content
, which has following structure:
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">269</int></lst>
</response>
I am interested in the two int
fields (here with the values 0
and 269
).
I load the xml via:
$xml = simplexml_load_string($content);
I then expected that:
$fields = $xml->xpath('int');
would give me an array of those fields, alas, it is empty.
What am I doing wrong? How can I parse the int fields in this example?
Upvotes: 0
Views: 31
Reputation: 13728
try
$xml ='<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">269</int></lst>
</response>';
$xml = simplexml_load_string($xml);
print_r($xml);
echo $field = $xml->lst->int[0];
echo $fields = $xml->lst->int[1];
For attributes:-
echo $attr1 = $xml->lst->int[0]->attributes();
echo $attr2 = $xml->lst->int[1]->attributes();
Upvotes: 1
Reputation: 2128
you forget slashes
$fields = $xml->xpath('//int');
also
$status= $xml->xpath('//int[@name="status"]');
$QTime= $xml->xpath('//int[@name="QTime"]');
Upvotes: 2
Reputation: 2606
You need to change your xpath expression little bit like that :
$fields = $xml->xpath('lst/int');
This will return complete array with node value and attribute name value. Then you can fetch them like :
$fields[0]->{0} AND $fields[1]->{0}
Upvotes: 1