Search code examples
phpxmlxmlreader

Get Child Data from large XML file


I have an large XML file (26,434 lines) that I'm working with.

Excerpt XML:

<row name="Scorned Syndicate" shortName="-SS-" allianceID="1778325832" executorCorpID="98020631" memberCount="29" startDate="2010-08-14 00:29:00">
    <rowset name="memberCorporations" key="corporationID" columns="corporationID,startDate">
        <row corporationID="98020119" startDate="2011-02-09 04:52:00" />
        <row corporationID="98020631" startDate="2011-02-23 23:55:00" />
    </rowset>
</row>

By searching here I've found how I can get the alliance name and short name but now I want the corporationID

$xml = new XMLReader();
$xml->XML( $xmlString );

while( $xml->read() ) {
  if( 'row' === $xml->name ) {
        echo $xml->getAttribute('name') . ' '. $xml->getAttribute('shortName').'<br>';

        $xml->next();
    }
}

I researched enough to know I need to utilize a streaming reader for efficiency but not sure how to get the last part of my data.


Solution

  • When you use the next() function in the XMLReader the subtrees are skipped. Your while statement should advance it through automatically. If you need to see output before the entire file is parsed you might consider ob_flush, otherwise it would be better to create an array so that the format is more customizable.