I have found the XMLDom functions in PHP 4 do actually work, but I found them difficult to work with. I wrote a wrapper class to make them a little easier.
<?
class clsXML {
var $lXML;
function clsXML() {
//$this->lXML = xmldoc("");
$this->lXML = new_xmldoc("1.0");
}
function loadFile($File) {
$this->lXML = xmldocfile( $File );
}
function get_content($parent)
{
$nodes = $parent->children();
while($node = array_shift($nodes))
if ($node->type == XML_TEXT_NODE)
return $node->content;
return "";
}
function root() {
return $this->lXML->root();
}
function get_content($parent)
{
$nodes = $parent->children();
while($node = array_shift($nodes))
if ($node->type == XML_TEXT_NODE)
return $node->content;
return "";
}
function find_content($parent,$name)
{
$nodes = $parent->children();
while($node = array_shift($nodes))
if ($node->name == $name)
return get_content($node);
return "";
}
function selectSingleNode($parent, $nodename) {
$nodes = $parent->children();
while ($node = array_shift($nodes))
{
if ($node->name == $nodename) {
return $node;
}
}
}
function selectNodes($parent, $nodename) {
$nodes = $parent->children();
$a = array();
while ($node = array_shift($nodes))
{
if ($node->name == $nodename) {
array_push($a, $node);
}
}
return $a;
}
}
With this class (I did not include all of it), you can use the following code to process your XML:
$XML->loadFile("MYFILE.XML");
$nCMD = $XML->root();
$nlPER = $XML->selectNodes($nCMD, "person");
while ($nPER = array_shift($nlPER)) {
$firstname = $XML->find_content($nPER, "firstname")
//do SQL stuff here
}