# Help need for converting xml to php

**URL:** https://forum.kirupa.com/t/help-need-for-converting-xml-to-php/267926
**Category:** Uncategorized
**Created:** [August 7, 2008, 11:49am UTC](https://forum.kirupa.com/t/help-need-for-converting-xml-to-php/267926 "2008-08-07T11:49:20Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![vetrib2w](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vetrib2w](https://forum.kirupa.com/u/vetrib2w)
#### Post date: [August 7, 2008, 11:49am UTC](https://forum.kirupa.com/t/help-need-for-converting-xml-to-php/267926/1 "2008-08-07T11:49:20Z")

</div>

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](http://www.w3.org/2001/XMLSchema-instance)” xmlns:ns1=“[http://www.ferrit.co.nz/XML/Order-0-4](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/XML/Order-0-4) [http://www.ferrit.co.nz/data/schemas/ProductOrder0\_4\_5.xsd](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”\>  
[COLOR=Red] \<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\>[/COLOR]  
\<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…

```php

$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 file ends here…  
**my issue:  
I can get all the values expect [COLOR=Red]billing address(firstname,lastname…Country) and shipping address (firstname,lastname…Country)…[/COLOR]  
i dont know how to write for it… i did new things but i failed…**  
**so,  
please help me…**
