I have an xml file that looks like that and I want to parse as a regular XML, tryng to loop the values, in particular the Cell
value.
<APIReport xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Rochester.Models">
<Column xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:string>Name</d2p1:string>
<d2p1:string>Surname</d2p1:string>
</Column>
<Dates>29-04-2016, 00:00:00 - 29-04-2016, 23:59:59</Dates>
<Id>1200</Id>
<Row>
<APIReportRow>
<Cell xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d4p1:string>Francesco</d4p1:string>
<d4p1:string>Delolli</d4p1:string>
</Cell>
</APIReportRow>
<APIReportRow>
<Cell xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d4p1:string>Roberto</d4p1:string>
<d4p1:string>Delolli</d4p1:string>
</Cell>
</APIReportRow>
</Row>
<Title>Nomi</Title>
So I tried to parse it in PHP with SimpleXML, using this simple code:
$xml = simplexml_load_string($xmlstring);
print_r($xml);
But the Cell
values are empty:
SimpleXMLElement Object
(
[Column] => SimpleXMLElement Object
(
)
[Dates] => 29-04-2016, 00:00:00 - 29-04-2016, 23:59:59
[Id] => 1200
[Row] => SimpleXMLElement Object
(
[APIReportRow] => Array
(
[0] => SimpleXMLElement Object
(
[Cell] => SimpleXMLElement Object
(
)
)
[1] => SimpleXMLElement Object
(
[Cell] => SimpleXMLElement Object
(
)
)
)
)
[Title] => Nomi
)
My question is simple: how may I get the Cell
value of the XML?
Thanks to @IMSoP, I got the solution:
$xml = simplexml_load_string($xmlstring);
$rows = $xml->Row->APIReportRow;
foreach ($rows as $row) {
$cell = $row->Cell->children('d4p1', true);
echo "Name: " . (string) $cell->string[0] . "\n";
echo "Surname: " . (string) $cell->string[1] . "\n";
}