/    Sign up×
Community /Pin to ProfileBookmark

SIMPLEXML in SIMPLEXML

This is my XML file (sample):

[CODE]<?xml version = “1.0” encoding=”Windows-1252″ standalone=”yes”?>
<AuditFile xmlns=”urn:OECD:StandardAuditFile-Tax:PT_1.03_01″>
<Customer>
<CustomerID>ISO07060440644,401551083</CustomerID>
<AccountID>Desconhecido</AccountID>
<CustomerTaxID>501882</CustomerTaxID>
<CompanyName>BAberta</CompanyName>
<BillingAddress>
<AddressDetail>Edifing</AddressDetail>
<City>Praiso 1</City>
<PostalCode>4000-29</PostalCode>
<Country>PT</Country>
</BillingAddress>
<Telephone>220046</Telephone>
<SelfBillingIndicator>0</SelfBillingIndicator>
</Customer>
</AuditFile>[/CODE]

This is my PHP code, …

[code=php]
$xmlReader = new XMLReader();
$xmlReader->open($FileName);
$tag = ‘Customer’;

while($xmlReader->read()) {
while ($tag == $xmlReader->name) {
// since XMLReader doesn’t really supply us with much of a usable
// API, we can convert the current node to an instace of `SimpleXMLElement`
$elem = new SimpleXMLElement($xmlReader->readOuterXML());
var_dump($elem);
echo “<br>”;
// now use SimpleXMLElement as you normally would.
foreach ($elem->children() as $child) {
echo $child->getName(). ‘: ‘. $child;
if ($child->getName()==’CustomerID’) {$id=$child;}
if ($child->getName()==’CompanyName’) {$CN=$child;}
if ($child->getName()==’CustomerTaxID’) {$CTID=$child;}
if ($child->getName()==’BillingAddress’)
{

echo ” -> “;

// CANOT BROWSE THIS TAG —- HELP!!! It only works up to here.

foreach ($BillingAddress->children() as $child2) {
if ($child2->getName()==’AddressDetail’) { echo $child2;}
}
echo ” <- “;
}
}
echo “<br>”;
$xmlReader->next($tag);
}
}[/code]

Since the file is very big, I am using XMLREADER + SIMPLEXML to open the file and give me the output on screen.
A simple ECHO will do, since I will put the values in variables and output then on a HTML table.

The problem I have is that I get a 2 SimpleXMLElements because of the subtag BillingAddress.
How can I get the tags below the BillingAddress????

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Error404Apr 12.2014 — I'm guessing that with XMLReader, it would read the BillingAddress node with all of its children in 1 line, so you could use simplexml's xpath function. For example:

[code=php]
$addressDetail = $simpleXML->xpath("/BillingAddress/AddressDetail"); // using relative paths
echo($addressDetail."<br />");
$city = $simpleXML->xpath("//AuditFile/Customer/BillingAddress/City"); // using absolute paths
echo($city."<br />");
[/code]


If you're going to handle information that's sensitive and large, such as auditing and tax IDs, you might want to consider a database.
×

Success!

Help @Mamede spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...