当前位置: 首页 > 知识库问答 >
问题:

PHP解析XML响应与simplexml_load_string()

史和泰
2023-03-14

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

以下是我的测试脚本:

<?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()有一些细节。但我收到了这个带有的响应,我不想首先执行查找/替换脚本。

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

共有1个答案

华懿轩
2023-03-14

我以前曾与SimpleXML和soap争论过,但发现使用XMLReader类要容易得多,而且一旦检索到数据,我就可以将数据(如CUSCounts之类的内容)转换为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);
}

我得到:

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
)
 类似资料:
  • 本文向大家介绍用PHP解析XML,包括了用PHP解析XML的使用技巧和注意事项,需要的朋友参考一下 XML数据提取可能是一项常见的任务,但是要直接使用此数据,您需要了解PHP如何解析XML。在PHP中解析XML涉及各种不同的功能,所有这些功能协同工作以从XML文档中提取数据。我将完成所有这些功能,并在最后将它们联系在一起。 xml_parser_create() 此函数用于创建解析器对象,该对象将

  • simplexml_load_string()似乎不是以下xml的工作形式 上面的xml是响应的一部分,因此当得到结果时,它只包含属性 结果是SimpleXMLElement的一个对象,只有一个属性数组 它没有任何与“Chat_valiable”或隐藏相关的内容。 能找个人帮忙吗

  • 其答复如下: 我相信问题出在响应前面的jsonFlickrApi上。 执行以下代码时: }

  • 我在将XML SOAP返回转换为相应的POJO类时遇到问题。XML返回如下所示: 我尝试使用Jackson XMLMAPER,但是在反序列化过程中,我不能把它作为根元素考虑返回节点。它将“信封”节点视为根节点。 我需要只提取返回节点并转换到我的pojo类。 另一个问题是“项目”节点应该是集合的一部分,但是没有对这些元素进行分组的父节点。 有没有人知道一个解析器可以对这种类型的xml进行反序列化?

  • 问题内容: 我在服务器上调用PHP cURL方法,响应为XML类型。cURL将输出(在删除标记之后)保存在标量类型变量中。有没有一种方法可以将其存储在对象/哈希/数组中,以便于解析? 问题答案:

  • 问题内容: 我使用kso​​ap2 lib从android客户端与SOAP Web服务进行通信。ksoap团队做的很棒,但是问题是,没有一个很好的例子说明如何在不同方面正确使用它。例如我得到以下数据的肥皂响应: 它是一个复杂的对象,或者是StatusSetting对象的集合。当我尝试获取SoapObject的属性时,只有1个属性,其中所有数据均为字符串。它也不能解析为json。令人难以置信的是,没