Pregunta

I am getting a SimpleXML object back from a server using Guzzle. The response printed as string looks like valid XML. But when I try to get the individual element values, I am getting blank.

Please help me find what the problem is.

My code is here as well as on Viper-7:

<?php
    $xml = '<?xml version="1.0" encoding="UTF-8"?>
<RespOrderHistory xmlns="http://www.example.com/testService/xsd/OrderHistoryResponse_v1.xsd" xmlns:typ="http://www.example.com/testService/xsd/Types_v1.xsd">
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Cancelled</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>638</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-08T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-08T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>138</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-07T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-07T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>133</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-07T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-07T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
  <OrdersList>
    <typ:OrderNumber>128</typ:OrderNumber>
    <typ:OrderSubmitDate>2014-04-07T00:00:00.000+05:30</typ:OrderSubmitDate>
    <typ:CurrentStatus>Active</typ:CurrentStatus>
    <typ:StatusDate>2014-04-07T00:00:00.000+05:30</typ:StatusDate>
  </OrdersList>
</RespOrderHistory>';

$sXml = simplexml_load_string ($xml);

foreach($sXml as $order){
    echo $order->getName()." ";
    print_r($order);
    echo "<br/>";
}
?>
¿Fue útil?

Solución

Note: By putting your code into your question, you will make it easier for others to help you.

$xml = simplexml_load_string($x); // assume XML in $x

You will need 2 nested loops to iterate. First, you will want to select the <OrdersList>-nodes correctly, see the outer loop.

foreach ($xml->OrdersList as $orders) {

The children of <OrdersList> are namespaced:
$order->OrderNumber doesn't work, but the children()-method does the job:

    foreach ($orders->children("typ", TRUE) as $name => $value) {

        echo "$name: $value <br />";

    }

}

see it working: http://viper-7.com/bS2pEJ

Recommended reading: http://www.php.net/manual/en/simplexmlelement.children.php

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top