1

I would like to parse and extract information from an XML file for example I would like to extract the following things:

  • uio, batchId and creationDate from the header

  • All the accountToken, Id, setId, Amount, etc from the body

  • batchCount and TotalAmount from the footer

This is my 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 and 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&amp;#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 Blue 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 Blue 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, I wrote this code to at least retrieve AccountToken and Id but I get a blank page:

<?php
$doc = new DOMDocument;
$doc->load("sample.xml");
$rows = $doc->getElementsByTagNameNS('http://www.localhost.com/platform', 'Instruction');
foreach ($rows as $row) {
$AToken = $row->getAttribute('accountToken');
$Id    = $row->getAttribute('Id');
var_dump($AToken, $Id);
}
?>

But I haven't been able to get anything from the XML file.

2
  • This is an odd XML file; child elements with same name as, but different function from, the parent? What does print_r($rows) show you before the foreach loop? Commented Oct 30, 2014 at 19:54
  • @miken32 print_r($rows) won't show anything because there are no nodes named Instruction in the document. Commented Oct 30, 2014 at 20:16

2 Answers 2

1

Should that be Instructions instead of Instruction in the getElementsByTagNameNS call?

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't answer the question (although the observation is correct!).
0

The XML document you have is a little odd as the top element is same as its direct children. For this reason, I would use DOMXPath to retrieve the elements you want from the document (OK, and XPaths are awesome!). To use DOMXPath, you'll need to create a new DOMXPath object and register the namespace, http://www.localhost.com/platform, so it can search those elements.

NB: your script wasn't working because there are no Instruction elements in the document--they're all Instructions. :)

Here's a simple and easily-extensible script that will pull data out of the document that you've posted. It just prints the data, but you will probably want to do something a bit more fancy with it.

$doc = new DOMDocument;
$doc->load( $your_xml_here );

# create the DOMXPath object
$xp = new DOMXPath($doc);
# registers the namespace; to search for nodes in this namespace, prefix them with "c"
$xp->registerNamespace("c", 'http://www.localhost.com/platform');

# search for all c:Header nodes under the top node, c:Instructions
foreach ($xp->query("/c:Instructions/c:Header") as $h) {
    # array of attributes to retrieve...
    foreach (array('uio', 'batchId', 'creationDate') as $ha) {
        print "header attribute $ha: " . $h->getAttribute($ha) . PHP_EOL;
    }
}

# retrieves c:Instructions nodes that are under the c:Instructions node
foreach ($xp->query("/c:Instructions/c:Instructions") as $i) {
    # you can expand the list of attributes here
    foreach (array("Id", "accountToken") as $ia) {
        print "Instruction attrib $ia: " . $i->getAttribute($ia) . PHP_EOL;
    }
}

# footer information    
foreach ($xp->query("/c:Instructions/c:Footer") as $f) {
    foreach (array("batchCount", "totalAmount") as $fa) {
        print "footer attribute $fa: " . $f->getAttribute($fa) . PHP_EOL;
    }
}

Output for the XML you 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 all the attributes, it might be quicker to run the equivalent code using SimpleXMLElement:

$sxe = new SimpleXMLElement( $xml_source );
$sxe->registerXPathNamespace("c", 'http://www.localhost.com/platform');

# e.g. get the header data
foreach ($sxe->xpath("/c:Instructions/c:Header") as $i) {
    # iterate through all the element attributes
    foreach ($i->attributes() as $name => $value) {
        print "header attribute $name is $value" . PHP_EOL;
    }
}

Output:

header attribute uio is a881-aa05-1231391408a2
header attribute batchId is c7-8ef6-eb81b345e736
header attribute creationDate is 2014-08-10T00:00:00.000Z

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.