The parser i've used to date was from the php.net manual page, i'll paste it below.
Code: Select all
<?php
/*This XML2Array Class came from a user-comment on the XML page of php.net
Usage
Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
$objXML = new xml2Array();
$arrOutput = $objXML->parse($strYourXML);
print_r($arrOutput); //print it out, or do whatever!
*/
class xml2Array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
function parse($strInputXML) {
$this->resParser = xml_parser_create ();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->resParser, "tagData");
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)));
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($this->arrOutput,$tag);
}
function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
}
else {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
}
}
}
function tagClosed($parser, $name) {
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
array_pop($this->arrOutput);
}
}
?>
Code: Select all
$output[0]['children'][2]['children'][0]['tagData'];An example section is :
Code: Select all
<?xml version="1.0"?>
<!DOCTYPE ICECAT-interface SYSTEM "http://data.icecat.biz/dtd/ICECAT-interface_response.dtd">
<ICECAT-interface>
<Response ID="50425970" Status="1" Date="Tue Jan 22 04:42:15 2008" Request_ID="1200973335">
<CategoriesList>
<Category ID="559" UNCATID="45111601" Searchable="0" ThumbPic="http://data.icecat.biz/thumbs/CAT559.jpg" Score="534" LowPic="http://data.icecat.biz/img/low_pic/559-250.jpg">
<ParentCategory ID="558">
<Names>
<Name ID="1117" langid="1">projectors</Name>
<Name ID="1118" langid="2">projectoren</Name>
<Name ID="6610" langid="3">projecteurs</Name>
<Name ID="14453" langid="4">Projektoren</Name>
<Name ID="107552" langid="5">proiettori</Name>
<Name ID="22717" langid="6">proyectores</Name>
<Name ID="34514" langid="7">projektorer</Name>
<Name ID="51741" langid="8">?????????</Name>
<Name ID="86592" langid="12">???</Name>
</Names>
</ParentCategory>
<Keywords ID="2926" Value="" langid="1"/>
<Keywords ID="2927" Value="" langid="2"/>
<Keywords ID="2928" Value="" langid="3"/>
<Keywords ID="2929" Value="" langid="4"/>
<Keywords ID="2930" Value="" langid="5"/>
<Keywords ID="2931" Value="" langid="6"/>
<Name ID="1119" Value="pointers" langid="1"/>
<Name ID="1120" Value="pointers" langid="2"/>
<Name ID="6611" Value="pointers" langid="3"/>
<Name ID="14454" Value="Pointer" langid="4"/>
<Name ID="107553" Value="puntatori" langid="5"/>
<Name ID="22718" Value="indicadores" langid="6"/>
<Name ID="34515" Value="pointers" langid="7"/>
<Name ID="51742" Value="???????? ?????????" langid="8"/>
<Name ID="86593" Value="??" langid="12"/>
<Description ID="8182" Value="" langid="1"/>
<Description ID="8184" Value="" langid="2"/>
<Description ID="8183" Value="" langid="3"/>
<Description ID="14920" Value="" langid="4"/>
<Description ID="18707" Value="" langid="5"/>
<Description ID="22494" Value="" langid="6"/>
<Description ID="33697" Value="" langid="7"/>
</Category>There are other files for a product index, and then the product files themselves.
If anyone can give me some pointers on how to do this using the class above, or direct me to a better parser I can use, that would be much appreciated.
In addition to this, whats the best xml book? A book that gives a good introduction and grounding in XML but that is easy to understand and follow. I'll be using xml with php, so books that make use of the same pairing will be most useful.
I was thinking of getting "php in a nutshell" as it seems to have a good section on xml, php5 and oop (something else i need to look at in more detail, using 4 atm on work server).
*edit*
Found this at w3schools:
http://w3schools.com/php/php_xml_parser_expat.asp
not sure if it's the best way to do it. Will wait and see people's opinions.