how to get the xml values to php

Discussion in 'PHP' started by vetrivel, Aug 7, 2008.

  1. #1
    Hi Friends,
    I m having xml file and i need to convert it to php and then i need to insert it into mysql table.
    While doing so i come across few issues.and i dont know how to solve it.

    ................................this is my xml file..................................................
    <?xml version="1.0" encoding="UTF-8"?>
    <ns1:OrderBatch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://www.ferrit.co.nz/XML/Order-0-4" xsi:schemaLocation="http://www.ferrit.co.nz/XML/Order-0-4 http://www.ferrit.co.nz/data/schemas/ProductOrder0_4_5.xsd" >
    <ns1:Order FerritOrderID="393/3687.01" FerritRetailerID="ABC" CreationDate="2007-01-31T11:22:32Z" LastModifiedDate="2007-01-31T11:22:32Z" FerritCustomerID="393">
    <ns1:BillingAddress>
    <ns1:FirstName>Raymond1</ns1:FirstName>
    <ns1:LastName>Ying</ns1:LastName>
    <ns1:EMail>raymond.ying@telecom.co.nz</ns1:EMail>
    <ns1:phone>0272514517</ns1:phone>
    <ns1:FirstLine>35 Virginia Ave East</ns1:FirstLine>
    <ns1:SecondLine>Kingsland</ns1:SecondLine>
    <ns1:City>Auckland</ns1:City>
    <ns1:Region>Auckland Region</ns1:Region>
    <ns1:Country>NZ</ns1:Country>
    </ns1:BillingAddress>
    <ns1:ShippingAddress>
    <ns1:FirstName>Raymond</ns1:FirstName>
    <ns1:LastName>Ying</ns1:LastName>
    <ns1:EMail>raymond.ying@telecom.co.nz</ns1:EMail>
    <ns1:phone>0272514517</ns1:phone>
    <ns1:FirstLine>35 Virginia Ave East</ns1:FirstLine>
    <ns1:SecondLine>Kingsland</ns1:SecondLine>
    <ns1:City>Auckland</ns1:City>
    <ns1:Region>Auckland Region</ns1:Region>
    <ns1:Country>NZ</ns1:Country>
    </ns1:ShippingAddress>

    <ns1:payment Currency="NZD" GrandTotal="50.0" Refunded="0.0">
    <ns1:DPS PxToken=""></ns1:DPS>
    </ns1:payment>
    <ns1:ShippingMethod id="LineItemShipping_45BFC4BA-96CE-8E10-8585-AC1317A2EEC3" Total="10.0" Code="" Name="Basket Price" BasePrice="12.5" Discount="0.0"></ns1:ShippingMethod>
    <ns1:LineItems>
    <ns1:product id="LineItemProduct_45BFC4BA-2AA0-53F0-551B-AC1317A2EEE2" SKU="52" Name="ETNZ T-Shirt" Quantity="1.0" Total="40.0" BasePrice="40.0" Discount="0.0" TaxClass="/TaxMatrixNZ/normal" QuantityDispatched="0.0" QuantityRefunded="0.0"></ns1:product>
    </ns1:LineItems>
    <ns1:History>
    <ns1:Entry DateTime="2007-01-31T11:22:33Z" Visibility="CFR" Event="OrderReceived" Message="Your order has been sent to ABC Team NZ Store."></ns1:Entry>
    </ns1:History>
    </ns1:Order>
    </ns1:OrderBatch>
    ......................................This is my php file.................................................

    
    $dom=new DOMDocument();
    
    $dom->load('order.xml');
    
    $OrderBatch=$dom->getElementsByTagName('OrderBatch');
        foreach($OrderBatch as $OrderBatch){
            $xsi=$OrderBatch->getAttribute('xmlns:xsi');
            $ns1=$OrderBatch->getAttribute('xmlns:ns1');
            $schemaLocation=$OrderBatch->getAttribute('xsi:schemaLocation');
        }
    
    $Order=$dom->getElementsByTagName('Order');
        foreach($Order as $Order){
            $FerritOrderID=$Order->getAttribute('FerritOrderID');
            $FerritRetailerID=$Order->getAttribute('FerritRetailerID');
            $CreationDate=$Order->getAttribute('CreationDate');
            $LastModifiedDate=$Order->getAttribute('LastModifiedDate');
            $FerritCustomerID=$Order->getAttribute('FerritCustomerID');
        }
    
    $Payment=$dom->getElementsByTagName('Payment');
        foreach($Payment as $Payment){
            $Currency=$Payment->getAttribute('Currency');
            $GrandTotal=$Payment->getAttribute('GrandTotal');
            $Refunded=$Payment->getAttribute('Refunded');
        }
    
    $DPS=$dom->getElementsByTagName('DPS');
        foreach($DPS as $DPS){
            $PxToken=$DPS->getAttribute('PxToken');
        }
    
    $ShippingMethod=$dom->getElementsByTagName('ShippingMethod');
        foreach($ShippingMethod as $ShippingMethod){
            $Shipping_id=$ShippingMethod->getAttribute('id');
            $Shipping_Total=$ShippingMethod->getAttribute('Total');
            $Shipping_Code=$ShippingMethod->getAttribute('Code');
            $Shipping_Name=$ShippingMethod->getAttribute('Name');
            $Shipping_BasePrice=$ShippingMethod->getAttribute('BasePrice');
            $Shipping_Discount=$ShippingMethod->getAttribute('Discount');
        }
    
    $Product=$dom->getElementsByTagName('Product');
        foreach($Product as $Product){
            $Product_id=$Product->getAttribute('id');
            $Product_SKU=$Product->getAttribute('SKU');
            $Product_Name=$Product->getAttribute('Name');
            $Product_Quantity=$Product->getAttribute('Quantity');
            $Product_Total=$Product->getAttribute('Total');
            $Product_BasePrice=$Product->getAttribute('BasePrice');
            $Product_Discount=$Product->getAttribute('Discount');
            $Product_TaxClass=$Product->getAttribute('TaxClass');
            $Product_QuantityDispatched=$Product->getAttribute('QuantityDispatched');
            $Product_QuantityRefunded=$Product->getAttribute('QuantityRefunded');
       
        }
    
    $Entry=$dom->getElementsByTagName('Entry');
        foreach($Entry as $Entry){
            $DateTime=$Entry->getAttribute('DateTime');
            $Visibility=$Entry->getAttribute('Visibility');
            $Event=$Entry->getAttribute('Event');
            $Message=$Entry->getAttribute('Message');
           
        }
    
    PHP:

    ............................................php file ends here...................................
    my issue:
    I can get all the values expect billing address(firstname,lastname....Country) and shipping address (firstname,lastname....Country)..
    i dont know how to write for it.. i did new things but i failed..
    so,
    please help me.....
     
    vetrivel, Aug 7, 2008 IP
  2. BDazzler

    BDazzler Peon

    Messages:
    215
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    BDazzler, Aug 7, 2008 IP
  3. CarcaBot

    CarcaBot Active Member

    Messages:
    389
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #3
    or you can get using
    http://www.php.net/manual/en/function.simplexml-load-file.php

    
    <?php
    // The file test.xml contains an XML document with a root element
    // and at least an element /[root]/title.
    
    if (file_exists('test.xml')) {
        $xml = simplexml_load_file('test.xml');
     
        print_r($xml);
    } else {
        exit('Failed to open test.xml.');
    }
    ?>
    
    
    PHP:
    or this exg.:
    
    <?php
    $xml = '<?xml version="1.0" encoding="UTF-8" ?>
    <rss>
        <channel>
            <item>
                <title><![CDATA[Tom & Jerry]]></title>
            </item>
        </channel>
    </rss>';
    
    $xml = simplexml_load_string($xml);
    
    // echo does the casting for you
    echo $xml->channel->item->title;
    
    // but vardump (or print_r) not!
    var_dump($xml->channel->item->title);
    
    // so cast the SimpleXML Element to 'string' solve this issue
    var_dump((string) $xml->channel->item->title);
    ?> 
    PHP:
     
    CarcaBot, Aug 7, 2008 IP