Search code examples
phpxmlxmlreader

Correctly processing XML files using XMLReader


For personal use I am currently writing a recursive PHP function that should process a big amount of relatively big XML files. As I only need the value of 2-3 nodes I searched for a way to make this as memory- and traffic-saving as possible. So I've stumbled upon the PHP Object XMLReader, which doesn't quite work as expected and isn't really documented that well on PHP.net. My current code is the following:

<?php
    $reader = new XMLReader();
    $reader->open('http://path.to.xml/file', 'UTF-8');
    $att1 = $reader->getAttribute('att1');
    $att2 = $reader->getAttribute('att2');
    $reader->close();
    echo var_dump($att1) . ' ' . var_dump($att2);
?>

As you can see this is pure debug, but it just echoes out NULL two times, which, according to the docs, is the case "if no attribute with the given name [are] found or [...] positioned on an element node."

The XML structure (actual file is bigger than 500 lines, but also contains CDATA tags sometimes):

<parentnode>
    <att1><![CDATA[ VAL1 ]]></att1>
    <randomtags>randomval</randomtags>
    <att2>VAL2</att2>
    <foo>bar</foo>
</parentnode>

I have almost no experience with XML and previously only used SimpleXML - once. I don't want to parse the whole file, just these two values.

What I already tried:

  • encapsing the getAttribute parameter in < and >
  • statically calling the getAttribute Methods
  • changing the getAttribute parameter to include the parent node in multiple ways

Any tips or hints are welcome. :)


Solution

  • You're using

    $att1 = $reader->getAttribute('att1'); 
    

    but in your XML definition, att1 and att2 are elements, not attributes