Parsing a complicated XML file with PHP using DOM -
Parsing a complicated XML file with PHP using DOM -
i parse , extract info xml file illustration extract next things:
uio
, batchid
, creationdate
header
all accounttoken
, id
, setid
, amount
, etc body
batchcount
, totalamount
footer
this xml file:
<?xml version="1.0" encoding="utf-8"?> <c:instructions xmlns:c="http://www.localhost.com/platform"> <c:header uio="a881-aa05-1231391408a2" batchid="c7-8ef6-eb81b345e736" creationdate="2014-08-10t00:00:00.000z" /> <c:instructions accounttoken="0001578066518896635248066746078163233357907196" id="4178- a6dd-d1459cda71c3" setid="132530196846" amount="27.00" description="goulsalons , spas" timestamp="2014-08-10t05:37:56.000z" transactionid="1324300196883" transactiontimestamp="2014-08-07t18:32:30.000z" merchant="1307" consumer="1_4f13eb-4efb-b450- ca747763fbc4" store="363" campaign="partner, parnd spas, partner, pilot, 5/30/14" /> <c:instructions accounttoken="000227229359641325887385737985006" id="-08eb-43dd-884b-ccae980372f8" setid="2271109667569" amount="12.24" description="pyro's pi" timestamp="2014-08-10t03:00:05.000z" transactionid="291153267592" transactiontimestamp="2014-08-07t00:00:00.000z" merchant="13" consumer="0d3-4ef3-8922-932f0d860012" store="31" campaign=" challenge pyro&#39;s partner, pilot, 4/4/14" /> <c:instructions accounttoken="0002108430726669005078952425" id="bf48-4f86-84f6-df69432ef65b" setid="1211100232621" amount="26.95" description="blue" timestamp="2014-08-10t05:37:20.000z" transactionid="121030232642" transactiontimestamp="2014-08-07t17:48:29.000z" merchant="104880" consumer="2-4d32-a2b4-f0b54a8e50b5" store="39" campaign="partner challenge bluish fin, pilot, 5/30/14" /> <c:instructions accounttoken="000341863769868297728447318744937673" id="bf48-4f86-84f6-df69432ef65b" setid="1260320211819" amount="52.00" description="fin" timestamp="2014-08-10t05:37:41.000z" transactionid="1259211836" transactiontimestamp="2014-08-08t02:41:47.000z" merchant="180" consumer="6be4-46cd-95b8-244ab78c50ce" store="52" campaign="partner challenge bluish fin, partner, pilot, 5/30/14" /> <c:instructions accounttoken="000521692104031759552776822005" id="42f0-4850-9e33-54e7d79927d9" setid="29126329667269" amount="17.00" description=" bear" timestamp="2014-08-10t03:00:05.000z" transactionid="291259667289" transactiontimestamp="2014-08-08t00:00:00.000z" merchant="137" consumer="71bb-46d2-8e42-c9798d7dd0d7" store="39" campaign="partner challenge blind bear, partner, pilot, 5/22/14" /> <c:instructions accounttoken="0005216177101271759552776822005" id="42f0-4850-9e33-54e7d79927d9" setid="29134327117182" amount="9.00" description="bear" timestamp="2014-08-10t03:00:05.000z" transactionid="29124667297" transactiontimestamp="2014-08-08t00:00:00.000z" merchant="132" consumer="71bb-46d2-8e42-c9798d7dd0d7" store="398" campaign=" bear, partner, pilot, 5" /> <c:footer batchcount="6" totalamount="144" /> </c:instructions>
so, wrote code @ to the lowest degree retrieve accounttoken
, id
blank page:
<?php $doc = new domdocument; $doc->load("sample.xml"); $rows = $doc->getelementsbytagnamens('http://www.localhost.com/platform', 'instruction'); foreach ($rows $row) { $atoken = $row->getattribute('accounttoken'); $id = $row->getattribute('id'); var_dump($atoken, $id); } ?>
but haven't been able xml file.
the xml document have little odd top element same direct children. reason, utilize domxpath retrieve elements want document (ok, , xpaths awesome!). utilize domxpath, you'll need create new domxpath object , register namespace, http://www.localhost.com/platform
, can search elements.
nb: script wasn't working because there no instruction
elements in document--they're instructions
. :)
here's simple , easily-extensible script pull info out of document you've posted. prints data, want bit more fancy it.
$doc = new domdocument; $doc->load( $your_xml_here ); # create domxpath object $xp = new domxpath($doc); # registers namespace; search nodes in namespace, prefix them "c" $xp->registernamespace("c", 'http://www.localhost.com/platform'); # search c:header nodes under top node, c:instructions foreach ($xp->query("/c:instructions/c:header") $h) { # array of attributes retrieve... foreach (array('uio', 'batchid', 'creationdate') $ha) { print "header attribute $ha: " . $h->getattribute($ha) . php_eol; } } # retrieves c:instructions nodes under c:instructions node foreach ($xp->query("/c:instructions/c:instructions") $i) { # can expand list of attributes here foreach (array("id", "accounttoken") $ia) { print "instruction attrib $ia: " . $i->getattribute($ia) . php_eol; } } # footer info foreach ($xp->query("/c:instructions/c:footer") $f) { foreach (array("batchcount", "totalamount") $fa) { print "footer attribute $fa: " . $f->getattribute($fa) . php_eol; } }
output xml posted:
header attribute uio: a881-aa05-1231391408a2 header attribute batchid: c7-8ef6-eb81b345e736 header attribute creationdate: 2014-08-10t00:00:00.000z instruction attrib id: 4178- a6dd-d1459cda71c3 instruction attrib accounttoken: 0001578066518896635248066746078163233357907196 instruction attrib id: -08eb-43dd-884b-ccae980372f8 instruction attrib accounttoken: 000227229359641325887385737985006 instruction attrib id: bf48-4f86-84f6-df69432ef65b instruction attrib accounttoken: 0002108430726669005078952425 instruction attrib id: bf48-4f86-84f6-df69432ef65b instruction attrib accounttoken: 000341863769868297728447318744937673 instruction attrib id: 42f0-4850-9e33-54e7d79927d9 instruction attrib accounttoken: 000521692104031759552776822005 instruction attrib id: 42f0-4850-9e33-54e7d79927d9 instruction attrib accounttoken: 0005216177101271759552776822005 footer attribute batchcount: 6 footer attribute totalamount: 144
addendum: if you're getting attributes, might quicker run equivalent code using simplexmlelement:
$sxe = new simplexmlelement( $xml_source ); $sxe->registerxpathnamespace("c", 'http://www.localhost.com/platform'); # e.g. header info foreach ($sxe->xpath("/c:instructions/c:header") $i) { # iterate through element attributes foreach ($i->attributes() $name => $value) { print "header attribute $name $value" . php_eol; } }
output:
header attribute uio a881-aa05-1231391408a2 header attribute batchid c7-8ef6-eb81b345e736 header attribute creationdate 2014-08-10t00:00:00.000z
php xml parsing
Comments
Post a Comment