PHP - parse data from a SOAP response -
i'm using w3 validator api, , kind of response:
<?xml version="1.0" encoding="utf-8"?> <env:envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:body> <m:markupvalidationresponse env:encodingstyle="http://www.w3.org/2003/05/soap-encoding" xmlns:m="http://www.w3.org/2005/10/markup-validator"> <m:uri>http://myurl.com/</m:uri> <m:checkedby>http://validator.w3.org/</m:checkedby> <m:doctype>-//w3c//dtd xhtml 1.1//en</m:doctype> <m:charset>utf-8</m:charset> <m:validity>false</m:validity> <m:errors> <m:errorcount>1</m:errorcount> <m:errorlist> <m:error> <m:line>7</m:line> <m:col>80</m:col> <m:message>character data not allowed here</m:message> <m:messageid>63</m:messageid> <m:explanation> <![cdata[ page html here ]]> </m:explanation> <m:source><![cdata[ html again ]]></m:source> </m:error> ... </m:errorlist> </m:errors> <m:warnings> <m:warningcount>0</m:warningcount> <m:warninglist> </m:warninglist> </m:warnings> </m:markupvalidationresponse> </env:body> </env:envelope>
how can extract variables there?
i need validity
, errorcount
, if possible list of errors: line
, col
, , message
:)
is there easy way this?
you can load xml string simplexmlelement
simplexml_load_string
, find attributes using xpath. it's important register namespaces involved registerxpathnamespace
before using xpath.
$xml = file_get_contents('example.xml'); // $xml should xml source string $doc = simplexml_load_string($xml); $doc->registerxpathnamespace('m', 'http://www.w3.org/2005/10/markup-validator'); $nodes = $doc->xpath('//m:markupvalidationresponse/m:validity'); $validity = strval($nodes[0]); echo 'is valid: ', $validity, "\n"; $nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorcount'); $errorcount = strval($nodes[0]); echo 'total errors: ', $errorcount, "\n"; $nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorlist/m:error'); foreach ($nodes $node) { $nodes = $node->xpath('m:line'); $line = strval($nodes[0]); $nodes = $node->xpath('m:col'); $col = strval($nodes[0]); $nodes = $node->xpath('m:message'); $message = strval($nodes[0]); echo 'line: ', $line, ', column: ', $col, ' message: ', $message, "\n"; }
Comments
Post a Comment