我迫切需要知道如何阅读以下XML文件:
<uclassify xmlns="http://api.uclassify.com/1/ResponseSchema" version="1.00">
<status success="true" statusCode="2000"/>
<readCalls>
<classify id="cls1">
<classification>
<class className="negative" p="0.741735"/>
<class className="positive" p="0.258265"/>
</classification>
</classify>
</readCalls>
</uclassify>
我需要知道以下内容:
$status_code = ... (should be 2000)
$negative = ... (the value of p, should be 0.741735)
$positive = ... (the value of p, should be 0.258265)
祝你好运
安德烈
答案 0 :(得分:1)
$xml= simplexml_load_file('temp.xml');
foreach($xml->status->attributes() as $name => $value ){
echo $name.' '.$value.'<br>';
}
foreach($xml->readCalls->classify->classification->children() as $node ){
foreach($node->attributes() as $name => $value)
echo $name.' '.$value.'<br>';
}
O / P:
成功是真的 statusCode 2000 className否定 p 0.741735 className为正 p 0.258265
如果您想存储它们:
$status_code = (string)$xml->status->attributes()->statusCode;
foreach($xml->readCalls->classify->classification->class as $node ){
${(string) $node->attributes()->className} = (string) $node->attributes()->p;
}
echo $status_code.' '.$positive.' '.$negative;
O / P:
2000 0.258265 0.741735