Weedpacket;11017809 wrote:If you want to keep memory usage light on a large XML input, then the streaming [man]XML[/man] parser may be a better choice.
I definitely agree, although I took the lazy approach and illustrate my point using what I know: DOMDocument.
Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.
Although the actual problem here seems to be that it only retrieves elements of d, not nodes. Bug? Since I'm not familiar with SimpleXML there may be some other method of accessing child nodes, rather than child elements, but I failed finding one with a quick glance in the manual.
echo '<pre>'; print_r($xml); echo '</pre>';
$data = $xml->d->children();
foreach ($data as $n)
{
echo '<pre>'; echo ++$i . ' ' . print_r($n, 1).PHP_EOL; echo '</pre>';
}
I'd have expected 3 iterations with output for
$d-> first child node (TextNode)
$d-> second child (c element)
$d-> last child node (TextNode)
Similarly, assigning not $xml->d->children to $data, but rather $xml->d ought to assign the d element, on which an echo should produce its node value (which is all the text nodes contained in d directly as well as all the text nodes of d's child elements).
s0me0ne;11017805 wrote:
Here is my sample XML file, as you can see the D node doesn't have any text and neither does the C element...
If you're talking about the results using simpleXML in your example, then I agree. But if you're talking about thow things really are, then this should be corrected to:
The D node has 3 child nodes: A newline (LF) text node, followed by an empty c element, followed by a "bird food\n" text node.
And, demonstrating this using Dom
$xml = new DOMDocument('1.0');
$xml->loadXml($string);
echo '<pre>'; print_r($xml); echo '</pre>';
$a = $xml->childNodes->item(0);
$d = $a->getElementsByTagName('d')->item(0);
echo '<pre>';
$i = 0;
foreach ($d->childNodes as $child)
{
echo ++$i . ': ' . $child->nodeValue . PHP_EOL;
}
echo '</pre>';
Output
1:
2:
3: bird food
Or more simply
$d = $a->getElementsByTagName('d')->item(0);
echo "<pre>'". $d->nodeValue . "'</pre>"