I'm trying to write an XMLReader/SimpleXML hybrid function to read a very large (700MB) XML file. The XML is in this format:
<Items>
<Item>
<ItemKey>ABCDEF123</ItemKey>
<Name>
<English>An Item Name</English>
<German>An Item Name In German</German>
<French>An Item Name In French</French>
</Name>
<Description>
<English>An Item Description</English>
<German>An Item Description In German</German>
<French>An Item Description In French</French>
</Description>
</Item>
<Item>
<ItemKey>GHIJKL456</ItemKey>
<Name>
<English>Another Item Name</English>
<German>Another Item Name In German</German>
<French>Another Item Name In French</French>
</Name>
<Description>
<English>Another Item Description</English>
<German>Another Item Description In German</German>
<French>Another Item Description In French</French>
</Description>
</Item>
</Items>
The code I have written so far to do this:
$xml = new XMLReader();
if(!$xml->open('testitems.xml')){
die('Failed to open file!');
} else {
echo 'File opened';
}
$items = array();
while ($xml->read()){
if($xml->nodeType == XMLReader::ELEMENT){
if ($xml->name == 'Item'){
$item = array();
}
if ($xml->name == 'ItemKey'){
$xml->read();
$item['itemKey'] = $xml->value;
}
if ($xml->name == 'Name'){
$sxml = new SimpleXMLElement($xml->readOuterXml());
$englishName = $sxml->English;
$item['englishName'] = $englishName;
}
}
if($xml->nodeType == XMLReader::END_ELEMENT){
if ($xml->name == 'Item'){
$items[] = $item;
}
}
}
var_dump($items);
$xml->close();
However, while the ItemKey node value is being inserted into the array, the English Name is not, I can't seem to access this node properly. I would just use XMLReader for everything but since there are repeat occurences of the English node (one for Name, another for Description) from my Googling so far SimpleXML seemed the way forward, but no joy as yet.
Any suggestions? Any good guides? XMLReader documentation on php.net is woefully lacking in comparison to many other PHP features, and in general it seems hard to find detailed guides that are clear and concise.