2012-05-24 98 views
0

由於某種原因,我遇到了解析錯誤。我已經縮小到「什麼」,但不是「爲什麼」。PHP使用simplexml_load_string解析XML響應()

這裏是我的測試腳本:

<?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,提取裏面的數據。

+0

在我的回答下面我忘了提,爲什麼你的代碼是不工作!對於那個很抱歉。它實際上與命名空間和一些肥皂怪癖有關。看到這篇文章的更多信息:http://stackoverflow.com/questions/740730/parse-an-xml-with-simplexml-which-has-multiple-namespaces – EmmanuelG

回答

0

我之前曾經用SimpleXML和soap來處理過,但是發現使用XMLReader class更容易,我可以在檢索數據時將數據的內容(比如你的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); 
} 

而且我得到了:

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 
)