PHP使用simplexml_load_string()解析XML响应

PHP使用simplexml_load_string()解析XML响应,php,simplexml,Php,Simplexml,由于某种原因,我遇到了一个解析错误。我把范围缩小到“什么”,而不是“为什么” 以下是我的测试脚本: <?php $xml_string = '<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

由于某种原因,我遇到了一个解析错误。我把范围缩小到“什么”,而不是“为什么”

以下是我的测试脚本:

<?php
$xml_string = '<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <AccountsGetXMLResponse xmlns="https://test.testsite.com/v3.0/">
            <AccountsGetXMLResult>
            <AccountsWSDS xmlns="">
                <CUSACCOUNTS><ACCOUNT_ID>6036335098370413</ACCOUNT_ID><ACTIVE_FLAG>N</ACTIVE_FLAG><IN_USE>Y</IN_USE><ACCOUNT_BALANCE>0</ACCOUNT_BALANCE></CUSACCOUNTS>
                <CUSACCOUNTS><ACCOUNT_ID>6036335098370414</ACCOUNT_ID><ACTIVE_FLAG>N</ACTIVE_FLAG><IN_USE>Y</IN_USE><ACCOUNT_BALANCE>0</ACCOUNT_BALANCE></CUSACCOUNTS>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>PageSize</INFO_CODE><VALUE>10</VALUE></META_INFO>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>PageNumber</INFO_CODE><VALUE>1</VALUE></META_INFO>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>RowCount</INFO_CODE><VALUE>200</VALUE></META_INFO>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>PageCount</INFO_CODE><VALUE>20</VALUE></META_INFO>
            </AccountsWSDS>
            </AccountsGetXMLResult>
            <rowCount>200</rowCount>
            <pageCount>20</pageCount>
        </AccountsGetXMLResponse>
    </soap:Body>
</soap:Envelope>'; 

if( ! $xml = simplexml_load_string( $xml_string ) ) 
{ 
    echo "Unable to load XML string"; 
} 
else 
{ 
    echo "XML String loaded successfully"; 
}
?>

通过上面的测试xml字符串,我得到了“无法…”条件。 然而,当我取出“”时,它工作了! 显然simplexml_load_string()有一些细节。但我收到了这个带有的响应,我不想首先执行查找/替换脚本


还有,我这样做对吗?最后,我需要开始解析CUSACCOUNTS,提取其中的数据。

我之前曾与SimpleXML和soap争论过,但发现使用这种方法更容易,一旦检索到数据,我就可以将数据的核心(如CUSACCOUNTS内容)转换为SimpleXML对象

以下是我在你的情况下所做的:

$xml_string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <AccountsGetXMLResponse xmlns="https://test.testsite.com/v3.0/">
            <AccountsGetXMLResult>
            <AccountsWSDS xmlns="">
                <CUSACCOUNTS><ACCOUNT_ID>6036335098370413</ACCOUNT_ID><ACTIVE_FLAG>N</ACTIVE_FLAG><IN_USE>Y</IN_USE><ACCOUNT_BALANCE>0</ACCOUNT_BALANCE></CUSACCOUNTS>
                <CUSACCOUNTS><ACCOUNT_ID>6036335098370414</ACCOUNT_ID><ACTIVE_FLAG>N</ACTIVE_FLAG><IN_USE>Y</IN_USE><ACCOUNT_BALANCE>0</ACCOUNT_BALANCE></CUSACCOUNTS>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>PageSize</INFO_CODE><VALUE>10</VALUE></META_INFO>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>PageNumber</INFO_CODE><VALUE>1</VALUE></META_INFO>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>RowCount</INFO_CODE><VALUE>200</VALUE></META_INFO>
                <META_INFO><INFO_CLASS>TableInfo</INFO_CLASS><INFO_TYPE>Accounts</INFO_TYPE><INFO_CODE>PageCount</INFO_CODE><VALUE>20</VALUE></META_INFO>
            </AccountsWSDS>
            </AccountsGetXMLResult>
            <rowCount>200</rowCount>
            <pageCount>20</pageCount>
        </AccountsGetXMLResponse>
    </soap:Body>
</soap:Envelope>
XML;

$xml = new XmlReader();
$xml->xml($xml_string);
while($xml->read()){
  // if the current item is an open tag AND matches the account response, grab it's xml
  if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == "AccountsGetXMLResponse") {
    // BAM! readOuterXML yanks the xml string out (including the element we matched)
    // so that we can convert it into a simplexml object for easy iterating
    $xml_obj = new SimpleXMLElement($xml->readOuterXML());
    break;
  }
}

// now we can iterate through it hooray!
foreach($xml_obj->AccountsGetXMLResult->AccountsWSDS->CUSACCOUNTS as $cusaccount){
  // I convert to array and print here but you can do w/e you want now
  print_r((array)$cusaccount);
}

在我下面的回答中,我忘了提到为什么你的代码不工作了!很抱歉。它实际上与名称空间和一些soap特性有关。有关更多信息,请参阅本文:
Array
(
    [ACCOUNT_ID] => 6036335098370413
    [ACTIVE_FLAG] => N
    [IN_USE] => Y
    [ACCOUNT_BALANCE] => 0
)
Array
(
    [ACCOUNT_ID] => 6036335098370414
    [ACTIVE_FLAG] => N
    [IN_USE] => Y
    [ACCOUNT_BALANCE] => 0
)