Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.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
Php 使用simplexml\u加载\u字符串将XML转换为数组_Php_Arrays_Xml_Simplexml - Fatal编程技术网

Php 使用simplexml\u加载\u字符串将XML转换为数组

Php 使用simplexml\u加载\u字符串将XML转换为数组,php,arrays,xml,simplexml,Php,Arrays,Xml,Simplexml,我需要将XML转换为数组,但它不能转换 这是我的密码 <?php $response='<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/s

我需要将XML转换为数组,但它不能转换

这是我的密码

<?php
$response='<?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>
<Search xmlns="http:url">
  <Request>
    <aaa>string</aaa>
    <bbb>string</bbb>
    <ccc>srting</ccc>
    <SourceName>string</SourceName>

  </Request>
</Search>
</soap:Body>
</soap:Envelope>';


function xml2Array($xmlstring)
{
    $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    return json_decode($json,TRUE);
}
$arr = xml2Array($response);
print_r($arr); 

尝试类似于此解决方案的方法

在您的情况下,请尝试此代码

<?php
$response='<?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>
<Search xmlns="http:url">
  <Request>
    <aaa>string</aaa>
    <bbb>string</bbb>
    <ccc>srting</ccc>
    <SourceName>string</SourceName>

  </Request>
</Search>
</soap:Body>
</soap:Envelope>';


function xml2Array($xmlstring)
{
    $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA, "http://schemas.xmlsoap.org/soap/envelope/");
    $xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
    $bodies = $xml->xpath('//soap-env:Body');
    if (is_array($bodies) && !empty($bodies[0])) {
        $json = json_encode($bodies[0]);
        return json_decode($json,TRUE);
    } else {
        return false;
    }
}
$arr = xml2Array($response);
print_r($arr);

不幸的是,当涉及名称空间时,SimpleXML不再简单。您可能无法使用
json\u encode($xml)
技巧。您真的需要为任何XML定义提供通用解决方案吗?@AlvaroGonzalez是否有其他替代方案?
<?php
$response='<?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>
<Search xmlns="http:url">
  <Request>
    <aaa>string</aaa>
    <bbb>string</bbb>
    <ccc>srting</ccc>
    <SourceName>string</SourceName>

  </Request>
</Search>
</soap:Body>
</soap:Envelope>';


function xml2Array($xmlstring)
{
    $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA, "http://schemas.xmlsoap.org/soap/envelope/");
    $xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
    $bodies = $xml->xpath('//soap-env:Body');
    if (is_array($bodies) && !empty($bodies[0])) {
        $json = json_encode($bodies[0]);
        return json_decode($json,TRUE);
    } else {
        return false;
    }
}
$arr = xml2Array($response);
print_r($arr);
Array
(
    [Search] => Array
        (
            [Request] => Array
                (
                    [aaa] => string
                    [bbb] => string
                    [ccc] => srting
                    [SourceName] => string
                )

        )

)