使用 simplexml 解析 xml 文件并访问属性

发布于 2024-10-09 16:03:43 字数 1693 浏览 5 评论 0原文

我正在尝试解析具有以下结构的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2024-10-16 16:03:43

试试这个:

$resXml = simplexml_load_file($requestUrl); //$requestUrl is where the xml file is located

foreach ($resXml->readCalls->classify->classification->class as $d) {
    $currClassificationName = $d['className'];
    $currClassificationRating = (float) $d['p'];
    echo "$currClassificationName: $currClassificationRating" . "</br>";
}

Try this out:

$resXml = simplexml_load_file($requestUrl); //$requestUrl is where the xml file is located

foreach ($resXml->readCalls->classify->classification->class as $d) {
    $currClassificationName = $d['className'];
    $currClassificationRating = (float) $d['p'];
    echo "$currClassificationName: $currClassificationRating" . "</br>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文