www.webdeveloper.com
Results 1 to 2 of 2

Thread: SIMPLEXML in SIMPLEXML

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Posts
    2

    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>
    This is my PHP code, ...

    PHP Code:
    $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);
            }

    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????

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    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:

    PHP Code:
    $addressDetail $simpleXML->xpath("/BillingAddress/AddressDetail"); // using relative paths
    echo($addressDetail."<br />");
    $city $simpleXML->xpath("//AuditFile/Customer/BillingAddress/City"); // using absolute paths
    echo($city."<br />"); 
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles