0

I'm running into a parse error for some reason. I've narrowed it down to the "what", but not the "why".

Here's my testing script:

<?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.

Also, am I doing this right? In the end I'll need to start parsing the CUSACCOUNTS, extracting the data inside.

hakre
  • 193,403
  • 52
  • 435
  • 836
coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
  • In my answer below I forgot to mention why your code isn't working! Sorry about that. It actually has to do with namespaces and some soap quirkiness. See this post for more info: http://stackoverflow.com/questions/740730/parse-an-xml-with-simplexml-which-has-multiple-namespaces – EmmanuelG May 24 '12 at 19:37

1 Answers1

0

I have wrangled with SimpleXML and soap before but found that using the XMLReader class was way easier and I could convert the guts of the data (like ur CUSACCOUNTS stuff) into a SimpleXML object once I retrieve it.

Here's what I did in your case:

$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);
}

And I got:

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
)
EmmanuelG
  • 1,051
  • 9
  • 14