Php 无法使用simplexml\u加载\u字符串分析XML

Php 无法使用simplexml\u加载\u字符串分析XML,php,simplexml,Php,Simplexml,我尝试了各种方法,如图所示 而且还有更多 我甚至试过这个函数 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/Save

我尝试了各种方法,如图所示 而且还有更多

我甚至试过这个函数

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>
我要么得到一个空数组,要么得到如下错误:

“致命错误:未捕获的异常”“异常”“带有消息”“字符串” 无法分析为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 />\n";
    }
$ReturnXML=新的simplexmlement($ReturnData);
foreach($ReturnXML->children('b',true)->条目作为$entries){
echo(string)“Summary:”.simplexml\u load\u string($entries->StatusCode->children()->asXML(),null,LIBXML\u NOCDATA)。“
\n”; }
我不知道您将如何使用
SimpleXMLElement
实现这一点,但从您尝试了这么多事情的事实来看,我相信所使用的实际方法并不重要,因此您应该发现以下内容,其中使用了
DOMDocument
DOMXPath

        /* The SOAP response */
        $strxml='<?xml version="1.0" encoding="UTF-8"?>
<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>';

        /* create the DOMDocument and manually control errors */
        libxml_use_internal_errors( true );
        $dom=new DOMDocument;
        $dom->validateOnParse=true;
        $dom->recover=true;
        $dom->strictErrorChecking=true;
        $dom->loadXML( $strxml );
        libxml_clear_errors();

        /* Create the XPath object */
        $xp=new DOMXPath( $dom );
        /* Register the various namespaces found in the XML response */
        $xp->registerNamespace('b','http://schemas.datacontract.org/2004/07/FooAPI.Entities.Foo');
        $xp->registerNamespace('i','http://www.w3.org/2001/XMLSchema-instance');
        $xp->registerNamespace('s','http://www.w3.org/2003/05/soap-envelope');
        $xp->registerNamespace('a','http://www.w3.org/2005/08/addressing');

        /* make XPath queries for whatever pieces of information you need */
        $Action=$xp->query( '//a:Action' )->item(0)->nodeValue;
        $StatusCode=$xp->query( '//b:StatusCode' )->item(0)->nodeValue;
        $StatusInformation=$xp->query( '//b:StatusInformation' )->item(0)->nodeValue;


        printf(
            "<pre>
            %s
            %s
            %s
            </pre>",
            $Action,
            $StatusCode,
            $StatusInformation
        );

我不知道如何使用
SimpleXMLElement
实现这一点,但从您尝试了这么多事情的事实来看,我相信实际使用的方法并不重要,因此您应该会发现以下内容,其中使用了
DOMDocument
DOMXPath

        /* The SOAP response */
        $strxml='<?xml version="1.0" encoding="UTF-8"?>
<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>';

        /* create the DOMDocument and manually control errors */
        libxml_use_internal_errors( true );
        $dom=new DOMDocument;
        $dom->validateOnParse=true;
        $dom->recover=true;
        $dom->strictErrorChecking=true;
        $dom->loadXML( $strxml );
        libxml_clear_errors();

        /* Create the XPath object */
        $xp=new DOMXPath( $dom );
        /* Register the various namespaces found in the XML response */
        $xp->registerNamespace('b','http://schemas.datacontract.org/2004/07/FooAPI.Entities.Foo');
        $xp->registerNamespace('i','http://www.w3.org/2001/XMLSchema-instance');
        $xp->registerNamespace('s','http://www.w3.org/2003/05/soap-envelope');
        $xp->registerNamespace('a','http://www.w3.org/2005/08/addressing');

        /* make XPath queries for whatever pieces of information you need */
        $Action=$xp->query( '//a:Action' )->item(0)->nodeValue;
        $StatusCode=$xp->query( '//b:StatusCode' )->item(0)->nodeValue;
        $StatusInformation=$xp->query( '//b:StatusInformation' )->item(0)->nodeValue;


        printf(
            "<pre>
            %s
            %s
            %s
            </pre>",
            $Action,
            $StatusCode,
            $StatusInformation
        );

方法1。

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

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;
}
方法2.


可以使用
print\r(XMLtoArray($xml))获取数组

方法1。

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

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;
}
方法2.


可以使用
print\r(XMLtoArray($xml))获取数组

你知道
pod
b
是一个名称空间,对吗?@u\u mulder是的,我试过声明一个名称空间,但没有用。你知道
pod
b
是一个名称空间,对吗?@u\u mulder是的,我试过声明一个名称空间,这看起来很有趣,但我选择了@rakesh jakkar的方法1,因为它是最短的。尽管如此,你还是得到了一张赞成票。:-)这看起来很有趣,但我选择了@rakesh jakkar的方法1,因为它是最短的。尽管如此,你还是得到了一张赞成票。:-)方法1对我来说很有效,因为我喜欢简单和简短,我把它与优雅的代码联系起来(或者可能是因为我懒惰),所以我没有超出这个范围。塔克斯!方法1对我来说很有效,因为我喜欢简单和简短,我把它与优雅的代码联系起来(或者可能是因为我懒惰),所以我没有超出这个范围。塔克斯!
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $indexes);// $xml containing the XML
xml_parser_free($p);
echo "Index array\n";
print_r($indexes);
echo "\nVals array\n";
print_r($values);
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;
}