0

How to read xml data in which no <? xml ?> tag is mentaioned and convert it into array, XML data comes from url, see my below XML data:

<Item>
  <itemID>140</itemID>
  <systemSku readonly="true">210000000140</systemSku>
  <defaultCost currency="USD">6.5</defaultCost>
  <avgCost currency="USD">6.5</avgCost>
  <tax>true</tax>
  <archived>false</archived>
  <itemType>default</itemType>
  <description>Adult Baltic Amber - 17 inch - Raw Round Lemon Necklace</description>
  <modelYear>0</modelYear>
  <upc></upc>
  <ean></ean>
  <customSku>BANK-RL-17R</customSku>
  <manufacturerSku></manufacturerSku>
  <timeStamp>2013-11-09T09:32:42+00:00</timeStamp>
  <categoryID>8</categoryID>
  <taxClassID>0</taxClassID>
  <departmentID>0</departmentID>
  <itemMatrixID>0</itemMatrixID>
  <manufacturerID>4</manufacturerID>
  <seasonID>0</seasonID>
  <defaultVendorID>0</defaultVendorID>
  <Prices>
    <ItemPrice>
      <amount currency="USD">32.95</amount>
      <useType readonly="true">Default</useType>
    </ItemPrice>
    <ItemPrice>
      <amount currency="USD">0</amount>
      <useType readonly="true">MSRP</useType>
    </ItemPrice>
  </Prices>
</Item>

I have used below code php for reading data:

   <?php            
//header('content-type: text/plain; charset=UTF-8');
mb_internal_encoding("UTF-8");
$url = "http://east6.merchantos.com/API/Account/59675/Item";

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$output = curl_exec($ch); // execute curl request
curl_close($ch);
$total_product_item = simplexml_load_string('<?xml version="1.0" ?>' .$output);
$total_product_item = object_to_array($total_product_item);
$total_product_item = $total_product_item['Item'];
//$count_products_qty = count($total_product_item); 
echo "<pre>"; print_r($total_product_item); echo "</pre>";


    function object_to_array($data) 
        {
                if ((! is_array($data)) and (! is_object($data))) return 'xxx'; //$data;

                $result = array();

                $data = (array) $data;
                foreach ($data as $key => $value) {
                    if (is_object($value)) $value = (array) $value;
                    if (is_array($value)) 
                    $result[$key] = object_to_array($value);
                    else
                    $result[$key] = $value;
                }

                return  $result;
        }

?>

Please help me out. Thanks !

1
  • Google PHP XML Parser. Commented Nov 11, 2013 at 14:16

1 Answer 1

1

If I understand your problem correct the missing <?xml version="1.0" ?> is the problem. Add it before your output and then parse it:

$total_product_item = simplexml_load_string('<?xml version="1.0" ?>' . $output);
Sign up to request clarification or add additional context in comments.

2 Comments

I have added this but still it gives below error and returning 'x'. Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in D:\xamppnew\htdocs\lightspeed\get_product_items.php on line 13 Warning: simplexml_load_string(): <?xml version="1.0" ?> in D:\xamppnew\htdocs\lightspeed\get_product_items.php on line 13 Warning: simplexml_load_string(): ^ in D:\xamppnew\htdocs\lightspeed\get_product_items.php on line 13 Warning: Illegal string offset 'Item' in D:\xamppnew\htdocs\lightspeed\get_product_items.php on line 15 x
It works for me with your test XML input. Show me your updated code.

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.