开发者

PHP - parse data from a SOAP response

I'm using the W3 validator API, and I get this 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 is not allowed here</m:message>
                <m:messageid>63</m:messageid>
                <m:explanation>  <![CDATA[
                 PAGE HTML IS HERE
                  ]]>
                </m:explanation>
                <m:source><![CDATA[ HTML AGAIN ]]></m:sour开发者_如何学Pythonce>
            </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 I extract some variables from there?

I need validity, errorcount and if possible from the list of errors: line, col, and message :)

Is there a easy way to do this?


You can load the XML string into a SimpleXMLElement with simplexml_load_string and then find the attributes using XPath. It's important to register the namespaces involved with registerXPathNamespace before using XPath.

$xml = file_get_contents('example.xml'); // $xml should be the 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 as $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";
}


You should be using a SOAP library to get this in the first place. There are various options you can try for this; nusoap, http://php.net/manual/en/book.soap.php, the zend framework also has SOAP client and server which you can use. Whatever implementation you use will allow you to get the data in some way. Doing a var_dump() on whatever holds the initial response should aid you in navigating through it.


If you rather use the DOMDocument class from php. You don't have to know Xpath to get this working. An example:

$url = "http://www.google.com";
$xml = new DOMDocument();
$xml->load("http://validator.w3.org/check?uri=".urlencode($url)."&output=soap12"); 

$doctype = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'doctype')->item(0)->nodeValue;
$valid = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'validity')->item(0)->nodeValue;
$errorcount = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'errorcount')->item(0)->nodeValue;
$warningcount = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'warningcount')->item(0)->nodeValue;

$errors = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'error');
foreach ($errors as $error) {
    echo "<br>line: ".$error->childNodes->item(1)->nodeValue;
    echo "<br>col: ".$error->childNodes->item(3)->nodeValue;
    echo "<br>message: ".$error->childNodes->item(5)->nodeValue;
}

// item() arguments are uneven because the empty text between tags is counted as an item.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜