使用 simplexml 解析 xml 文件并访问属性
我正在尝试解析具有以下结构的 xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
- <uclassify xmlns="http://api.uclassify.com/1/ResponseSchema" version="1.00">
<status success="true" statusCode="2000" />
- <readCalls>
- <classify id="cls1">
- <classification>
<class className="Arts" p="0.920034" />
<class className="Business" p="2.81823e-005" />
<class className="Computers" p="0.0040341" />
<class className="Games" p="0.00846496" />
<class className="Health" p="0.00203198" />
<class className="Home" p="0.00136572" />
<class className="Recreation" p="0.000526926" />
<class className="Science" p="0.000160703" />
<class className="Society" p="0.0611354" />
<class className="Sports" p="0.00221835" />
</classification>
</classify>
</readCalls>
</uclassify>
我正在尝试访问类中的属性 className 和 p。 这是我到目前为止所拥有的代码:
$resXml = simplexml_load_file($requestUrl); //$requestUrl is where the xml file is located
$children = $resXml->children('http://api.uclassify.com/1/ResponseSchema');
foreach ($children->readCalls->classify->classification->class as $d) {
$currClassificationName = $d['className'];
$currClassificationRating = (float) $d['p'];
echo "$currClassificationName: $currClassificationRating" . "</br>";
}
这是输出:
: 0
: 0
: 0
: 0
: 0
: 0
: 0
: 0
: 0
我想要得到的输出是:
Arts: 0.920034
Business: 2.81823e-005
...
Society: 0.0611354
Sports: 0.00221835
一段时间以来一直在尝试不同的事情,但无法弄清楚这一点。请帮忙。
I am trying to parse a xml file which has this structure:
<?xml version="1.0" encoding="UTF-8" ?>
- <uclassify xmlns="http://api.uclassify.com/1/ResponseSchema" version="1.00">
<status success="true" statusCode="2000" />
- <readCalls>
- <classify id="cls1">
- <classification>
<class className="Arts" p="0.920034" />
<class className="Business" p="2.81823e-005" />
<class className="Computers" p="0.0040341" />
<class className="Games" p="0.00846496" />
<class className="Health" p="0.00203198" />
<class className="Home" p="0.00136572" />
<class className="Recreation" p="0.000526926" />
<class className="Science" p="0.000160703" />
<class className="Society" p="0.0611354" />
<class className="Sports" p="0.00221835" />
</classification>
</classify>
</readCalls>
</uclassify>
I am trying to access the properties className and p in class.
This is the code that I have so far:
$resXml = simplexml_load_file($requestUrl); //$requestUrl is where the xml file is located
$children = $resXml->children('http://api.uclassify.com/1/ResponseSchema');
foreach ($children->readCalls->classify->classification->class as $d) {
$currClassificationName = $d['className'];
$currClassificationRating = (float) $d['p'];
echo "$currClassificationName: $currClassificationRating" . "</br>";
}
This is the output:
: 0
: 0
: 0
: 0
: 0
: 0
: 0
: 0
: 0
The output I am trying to get is:
Arts: 0.920034
Business: 2.81823e-005
...
Society: 0.0611354
Sports: 0.00221835
Been trying different things for a while and cannot figure this out. Please Help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this out: