Hullo, I am trying to parse data from a python script directly connected to a a server back-office in php. To do that I converted the received array to JSON and then, in a php script, I converted it into XML so to parse it on another server with file_get_html. All good and fine but for a tag issue. Basically the tags are numbered as in
<item0></item0><item1></item1>...
and if I use
foreach($html->find('item') as $element)
I find nothing.
Is there a way to either parse all the tags starting with item OR at least transform all the string of the kind into (even that would of course slow the processing, but anyway..) OR appropriately change all the relevant string names either in php or python in the various phases?
Thanks, Fabrizio
First do not load XML as HTML. This can destroy it.
It is possible to fetch all tags that start with an defined string.
$xml = <<<'XML'
<items>
<item0></item0>
<item1></item1>
</items>
XML;
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
$nodes = $xpath->evaluate('//*[starts-with(local-name(), "item")]');
foreach ($nodes as $node) {
var_dump($node->getNodePath());
}
But why do you number the tags? You are aware that XML allows you to have siblings with the same element name?
<items>
<item>...</item>
<item>...</item>
...
</items>
The Xpath for this structure would be /items/item
.
Or if you like to keep an array from JSON grouped in XML use a simple name like _ for child elements:
{
"title" : "foo",
"items" : [ 1 , 2, 3 ]
}
<json>
<title>foo</title>
<items>
<_>1</_>
<_>2</_>
<_>3</_>
</items>
</json>
The Xpath for this structure would be /json/items/*
or for a specific item /json/items/*[2]
.
The item issue was solved. array_to_xml apparently weirdly calls the tags it generates. When directly checking the dictionary no item tag exists.