2012-02-09 4 views
-1

私は早急に次のXMLファイルを読み取る方法を知っておく必要があります。PHPの複雑な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) 

敬具

アンドレ

+1

何を試しましたか? PHPの場合でも、XMLを解析するための十分な先例があります。 – deceze

+1

[PHPでXMLファイルから情報を外挿する方法]の複製が可能です(http://stackoverflow.com/questions/1183657/how-to-extrapolate-information-from-xml-file-in-php) – deceze

+0

['SimpleXML '](http://php.net/manual/en/book.simplexml.php)、それは[XPath](http://www.php.net/manual/en/simplexmlelement.xpath.php)エンジンが簡単に使えるあなたが望むことをやりなさい。 – DaveRandom

答えて

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

+0

答えをありがとう! – andrebruton

関連する問題