English 中文(简体)
PHP 解析带有简单xml_load_string () 的 XML 响应
原标题:PHP Parsing XML response with simplexml_load_string()

我遇到了一个分析错误,因为某种原因。我把它缩小到“什么”而不是“为什么”。

这是我的测试脚本:

<?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"; 
}
?>

With the testing xml string above I get the "Unable....." condition. However, when I take out the "" it works! Obviously simplexml_load_string() has some particulars. But I m receiving this response with the , and I don t want to have to do a find/replace script first.

而且,我这样做对吗?最后我需要开始分析CUSACCOUNTS, 提取里面的数据。

最佳回答

我以前用过简单XML和肥皂,但发现使用 XMLReader 类 很容易,一旦我检索到数据,我就可以将数据的内脏(如 ur CUSACCOUNTS ) 转换成一个简单XML 对象。

这是我对你的案子所做的:

$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
)
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签