Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解析RESTXML对PHP数组的响应_Php_Xml_Arrays_Rest_Response - Fatal编程技术网

如何解析RESTXML对PHP数组的响应

如何解析RESTXML对PHP数组的响应,php,xml,arrays,rest,response,Php,Xml,Arrays,Rest,Response,我正在使用REST构建一个Web服务。当客户端发送请求时,服务器将响应作为XML字符串发送回,如下所示 <?xml version="1.0" encoding="utf-8"?> <xml> <item> <user>1</user> <name>cym</name> <house_number>23423423</h

我正在使用REST构建一个Web服务。当客户端发送请求时,服务器将响应作为XML字符串发送回,如下所示

    <?xml version="1.0" encoding="utf-8"?>
    <xml>
      <item>
        <user>1</user>
         <name>cym</name>
       <house_number>23423423</house_number>
      <house_number_addition>sfsfsdf</house_number_addition>  
      <zipcode>erwer</zipcode>
      <city>werwer</city>
      <street>ertyu</street>
      <state_name>state1</state_name>
      <countryName>Albania</countryName>
      </item>
      </xml>

如果这不可能,那么如何获取属性的值。我使用simplexml\u load\u字符串尝试了这个方法,但它返回null。这是我的密码

  $response= $ex->getResponse();
  $xmldat=simplexml_load_string($response);
  $i= $xmldat->name;

SimpleXML允许您作为对象与XML交互,但它要求您指定每一代,从根元素开始,但不包括根元素(在您的例子中是

尝试:


这将返回一个SimpleXMLElement类型的对象,但如果您需要的话,可以很容易地将其转换为字符串。

有很多方法可以做到这一点

将其转换为数组

$xml = json_decode(json_encode((array) $xmldat), 1);
echo $xml['item']['name']; // according to your XML, u should use `item` first then the element


没什么问题,XML没有经过验证。下面的工作,你应该能够从那里建立

$xmldat = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<xml>
<item>
<user>1</user>
<name>cym</name>
<house_number>23423423</house_number>
<house_number_addition>sfsfsdf</house_number_addition>  
<zipcode>erwer</zipcode>
<city>werwer</city>
<street>ertyu</street>
<state_name>state1</state_name>
<countryName>Albania</countryName>
</item>
</xml>
XML;
$xml = simplexml_load_string($xmldat);
print_r($xml);
echo $xml->item->name; 

$xmldat=print$response并检查它是否有效xmli在给定的XML上手动尝试了它,它返回值fine..显示真正完整的XML
$i = $xmldat->list->name;
$xml = json_decode(json_encode((array) $xmldat), 1);
echo $xml['item']['name']; // according to your XML, u should use `item` first then the element
$response= $ex->getResponse();
$xmldat=simplexml_load_string($response);
$i= $xmldat->item->name;
$xmldat = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<xml>
<item>
<user>1</user>
<name>cym</name>
<house_number>23423423</house_number>
<house_number_addition>sfsfsdf</house_number_addition>  
<zipcode>erwer</zipcode>
<city>werwer</city>
<street>ertyu</street>
<state_name>state1</state_name>
<countryName>Albania</countryName>
</item>
</xml>
XML;
$xml = simplexml_load_string($xmldat);
print_r($xml);
echo $xml->item->name;