XML 使用 simplexml_load_string 排列
XML to array using simplexml_load_string
我需要将 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);
但是如果我删除
<soap:Body>
从 XML 开始它工作正常,有什么问题如何解决
尝试类似于此 question 的解决方案。
在你的情况下试试这个代码
<?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
)
)
)
我需要将 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);
但是如果我删除
<soap:Body>
从 XML 开始它工作正常,有什么问题如何解决
尝试类似于此 question 的解决方案。
在你的情况下试试这个代码
<?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
)
)
)