Attaining childnode values from XML

liunx

Guest
Attaining the child node values within the following Xml document, can some one show me what I am doing wrong I can navigate the XML document and return the values of the XML document but cant figure out how to return the child node values.

XML file :
<?xml version="1.0"?>
<dispatch>
<item>
<estamp>08-04-2006</estamp>
<project>X1800</project>
<dispatcher>andrew</dispatcher>
<docket>DCO1</docket>
<trailer_type>STD Oversize</trailer_type>
<trailer_rego>REGO 2345</trailer_rego>
<hotshot>YES</hotshot>
<loaded>
<loaditem>1162-1105-000A</loaditem>
<loaditem>1162-1105-000B</loaditem>
<loaditem>1162-1105-000C</loaditem>
<loaditem>1162-1105-000D</loaditem>
</loaded>
</item>
<item>
<estamp>01-04-2006</estamp>
<project>W1800</project>
<dispatcher>michael</dispatcher>
<docket>DOC123456</docket>
<trailer_type>STD Oversize</trailer_type>
<trailer_rego>REGO 123</trailer_rego>
<hotshot>YES</hotshot>
<loaded>
<loaditem>1158-1101-000A</loaditem>
<loaditem>1158-1101-000B</loaditem>
</loaded>
</item>
</dispatch>

php Code:

<?php
$dom = new DomDocument();
$dom->load("http://localhost/dispatch.xml");
$dom->preserveWhiteSpace = false;
$dispatched = $dom->documentElement;
$loaded = $dispatched->childNodes;
$i=-1;
$dispatch = $dispatched->getElementsByTagName("item");
foreach($dispatch as $item){
$nodeArray = array();
$i++;
$estamp = $dispatched->getElementsByTagName("estamp");
$entrydate = $estamp->item($i)->nodeValue;
$project = $dispatched->getElementsByTagName("project");
$projectno = $project->item($i)->nodeValue;
$dispatcher = $dispatched->getElementsByTagName("dispatcher");
$dispatchername = $dispatcher->item($i)->nodeValue;
$docket = $dispatched->getElementsByTagName("docket");
$docketno = $docket->item($i)->nodeValue;
$trailer_type = $dispatched->getElementsByTagName("trailer_type");
$trailertype = $trailer_type->item($i)->nodeValue;
$trailer_rego = $dispatched->getElementsByTagName("trailer_rego");
$trailerrego = $trailer_rego->item($i)->nodeValue;
$hotshot = $dispatched->getElementsByTagName("hotshot");
$hots = $hotshot->item($i)->nodeValue;
$a=-1;
$j=-1;
echo "$entrydate-$projectno-$dispatchername-$docketno-<BR>";
foreach ($loaded as $load) {
$a++;
$j++;
echo $load->item($a)->nodeValue;
echo $j."<br>";
}


echo "FINISHED READ ON<BR>";
}
?>I'm used to working with DOM in Javascript more than PHP, so I would probably do it similar to how I'd do it in Javascript:

<?php

$dom = new DomDocument();
$dom->load("http://localhost/dispatch.xml");
$dom->preserveWhiteSpace = false;

$items = $dom->documentElement->getElementsByTagName("item");

foreach ($items as $item) {
$estamp = $item->getElementsByTagName("estamp")->item(0);
$entrydate = $estamp->firstChild->nodeValue;

$project = $item->getElementsByTagName("project")->item(0);
$projectno = $project->firstChild->nodeValue;

$dispatcher = $item->getElementsByTagName("dispatcher")->item(0);
$dispatchername = $dispatcher->firstChild->nodeValue;

$docket = $item->getElementsByTagName("docket")->item(0);
$docketno = $docket->firstChild->nodeValue;

$trailer_type = $item->getElementsByTagName("trailer_type")->item(0);
$trailertype = $trailer_type->firstChild->nodeValue;

$trailer_rego = $item->getElementsByTagName("trailer_rego")->item(0);
$trailerrego = $trailer_rego->firstChild->nodeValue;

$hotshot = $item->getElementsByTagName("hotshot")->item(0);
$hots = $hotshot->firstChild->nodeValue;

echo "$entrydate :: $projectno :: $dispatchername :: $docketno :: $trailertype :: $trailerrego :: $hots<br />";

$loaded = $item->getElementsByTagName("loaded")->item(0);
$loaditems = $loaded->getElementsByTagName("loaditem");

foreach ($loaditems as $i => $loaditem) {
echo "$i :: " . $loaditem->firstChild->nodeValue . '<br />';
}

echo "FINISHED READ ON<br /><br />";
}

I don't think you need "->firstChild->nodeValue" in PHP, because on text nodes I think that just "->nodeValue" will work, but I just use "->firstChild->nodeValue" because that is how I'm used to doing it in javascript.If you are going to be using a DOMDocument, you might as well use XPath to query it, rather than pages of getElementsByTagName() calls. I'd try writing the query, but I'm not clear on what is wanted.I'm used to working with DOM in Javascript more than PHP, so I would probably do it similar to how I'd do it in Javascript:

<?php

$dom = new DomDocument();
$dom->load("http://localhost/dispatch.xml");
$dom->preserveWhiteSpace = false;

$items = $dom->documentElement->getElementsByTagName("item");

foreach ($items as $item) {
$estamp = $item->getElementsByTagName("estamp")->item(0);
$entrydate = $estamp->firstChild->nodeValue;

$project = $item->getElementsByTagName("project")->item(0);
$projectno = $project->firstChild->nodeValue;

$dispatcher = $item->getElementsByTagName("dispatcher")->item(0);
$dispatchername = $dispatcher->firstChild->nodeValue;

$docket = $item->getElementsByTagName("docket")->item(0);
$docketno = $docket->firstChild->nodeValue;

$trailer_type = $item->getElementsByTagName("trailer_type")->item(0);
$trailertype = $trailer_type->firstChild->nodeValue;

$trailer_rego = $item->getElementsByTagName("trailer_rego")->item(0);
$trailerrego = $trailer_rego->firstChild->nodeValue;

$hotshot = $item->getElementsByTagName("hotshot")->item(0);
$hots = $hotshot->firstChild->nodeValue;

echo "$entrydate :: $projectno :: $dispatchername :: $docketno :: $trailertype :: $trailerrego :: $hots<br />";

$loaded = $item->getElementsByTagName("loaded")->item(0);
$loaditems = $loaded->getElementsByTagName("loaditem");

foreach ($loaditems as $i => $loaditem) {
echo "$i :: " . $loaditem->firstChild->nodeValue . '<br />';
}

echo "FINISHED READ ON<br /><br />";
}

I don't think you need "->firstChild->nodeValue" in PHP, because on text nodes I think that just "->nodeValue" will work, but I just use "->firstChild->nodeValue" because that is how I'm used to doing it in javascript.

Worked perfectly thank you, I have converted it to use full php, thank you very much..I have to read each node and then store in a mysql database, the XML document resides on a handheld device created via a .NET app. I can read it all in via a Xpath query, but did not want to then convert it all back again with explode statements etc. I was about to create it using asp/vbscript but I try and avoid that at all costs if I can, as i prefer to use LAMP rather than anything else. It was the childnodes that threw me off, been a while since I last had to use XML.
 
Back
Top