无法使用implexml_Load_String解析XML

2022-04-13 00:00:00 php simplexml

我尝试了here中的各种方法 在here和更多的地方。

我甚至尝试了here中的函数。

该XML如下所示:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://tempuri.org/IFooEntryOperation/SaveFooStatusResponse</a:Action></s:Header><s:Body><SaveFooStatusResponse xmlns="http://htempuri.org/"><SaveFooStatusResult xmlns:b="http://schemas.datacontract.org/2004/07/FooAPI.Entities.Foo" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><b:AWBNumber>999999999</b:AWBNumber><b:IsError>true</b:IsError><b:Status><b:FooEntryStatus><b:StatusCode>Foo_ENTRY_FAILURE</b:StatusCode><b:StatusInformation>InvalidEmployeeCode</b:StatusInformation></b:FooEntryStatus></b:Status></SaveFooStatusResult></SaveFooStatusResponse></s:Body></s:Envelope>

这里是我的代码的一个示例(我有十几个变体):

$ReturnData = $row["ReturnData"]; // string frm a database
if (strpos($ReturnData, "s:Envelope") !== false){
    $ReturnXML = new SimpleXMLElement($ReturnData);
    $xml = simplexml_load_string($ReturnXML);
    $StatusCode = $xml["b:StatusCode"];
    echo "<br>StatusCode: " . $StatusCode;
    $IsError = $xml["b:IsError"];
    echo "<br>IsError: " . $IsError;
}

我尝试的另一个选项:

$test = json_decode(json_encode($xml, 1); //this didn't work either

我要么得到一个空数组,要么得到如下错误:

"致命错误:未捕获异常‘异常’,消息为‘字符串 无法分析为XML"

我尝试了太多事情,可能会忘记我的代码现在在哪里。请帮帮忙--我真的卡住了……

我也试过了:

$ReturnXML = new SimpleXMLElement($ReturnData);
    foreach( $ReturnXML->children('b', true)->entry as $entries ) {
        echo (string) 'Summary: ' . simplexml_load_string($entries->StatusCode->children()->asXML(), null, LIBXML_NOCDATA) . "<br />
";
    }

解决方案

方法1。

您可以尝试使用以下代码片段将其解析为数组

$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $indexes);// $xml containing the XML
xml_parser_free($p);
echo "Index array
";
print_r($indexes);
echo "
Vals array
";
print_r($values);

方法2.

function XMLtoArray($xml) {
  $previous_value = libxml_use_internal_errors(true);
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->preserveWhiteSpace = false; 
  $dom->loadXml($xml);
  libxml_use_internal_errors($previous_value);
  if (libxml_get_errors()) {
    return [];
  }
  return DOMtoArray($dom);
}
function DOMtoArray($root) {
  $result = array();
  if ($root->hasAttributes()) {
     $attrs = $root->attributes;
     foreach ($attrs as $attr) {
         $result['@attributes'][$attr->name] = $attr->value;
     }
  }
  if ($root->hasChildNodes()) {
    $children = $root->childNodes;
    if ($children->length == 1) {
        $child = $children->item(0);
        if (in_array($child->nodeType,[XML_TEXT_NODE,XML_CDATA_SECTION_NODE])) 
        {
            $result['_value'] = $child->nodeValue;
            return count($result) == 1
                ? $result['_value']
                : $result;
        }
    }
    $groups = array();
    foreach ($children as $child) {
        if (!isset($result[$child->nodeName])) {
            $result[$child->nodeName] = DOMtoArray($child);
        } else {
            if (!isset($groups[$child->nodeName])) {
                $result[$child->nodeName] = array($result[$child->nodeName]);
                $groups[$child->nodeName] = 1;
            }
            $result[$child->nodeName][] = DOMtoArray($child);
        }
    }
}
  return $result;
}

您可以使用print_r(XMLtoArray($xml));

获取数组

相关文章