开发者

Header attributes in xml doc

How do you get the attributes in the root node of a xml doc?

<xbox status="success" version="0开发者_JAVA技巧.0.9">
</xbox>

So I can check for a error:

if( ? == 'success'){
    //success
}else{
    //error
}


You cannot access the attributes of the root node directly from the DOMDocument. The workaround is to search for the tag first, then get the value:

$d = new DOMDocument;
$d->loadXML('<xml status="success" version="0.0.9"></xml>');

print $d->getElementsByTagName("xml")->item(0)->getAttribute("version");


The only attributes you may use in (and mind the question marks) header are "version" and "encoding".


Have you tried simple XML ( http://php.net/manual/fr/book.simplexml.php ) ?

Example ( adapted from a note of the manual ):

<?php
$sxml= new SimpleXmlElement($xmlstr);

if( (string) $sxml->attributes()->status== "success"){
    //success
}else{
    //error
}

?>


I just did this:

$xml = simplexml_load_string('<xbox status="success" version="0.0.9"></xbox>');
$status = $xml{status};

if($status == 'success'){
    echo 'Success!';
} else {
    echo 'Error!';
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜