在 PHP5 中解析复杂的多个 XML 属性

发布于 2024-08-22 00:57:53 字数 2367 浏览 6 评论 0原文

大家好,第一次在这个网站上发帖,请原谅格式。

我是用 php 解析 xml 的新手,这个 xml feed 让我难住了。我使用纯粹基于元素的格式,但属性并不那么复杂。我试图让网站更改为更简单的基于元素的格式,但他们坚持使用这种格式使用属性,我很难以列表/排名样式格式提取所有数据,特别是对于每个成员的排名和积分。

仅包含 2 个成员的 xml 文件示例,但实际上可能有 100 到 1000 个成员,具体取决于团队 ID。

<sitemembers>
  <members>
    <member>
      <name>username</name>
      <team id="1200">team1 name</team>
      <rank>
        <today rank="34" points="613.30" results="70"/>
        <yesterday rank="33" points="614.20" results="70"/>
        <lastweek rank="60" points="541.70" results="68"/>
        <lastmonth rank="163" points="415.60" results="63"/>
      </rank>
      <awards>
        <global gold="0" silver="0" bronze="0" medal="1"/>
        <local gold="5" silver="2" bronze="0" medal="3"/>
      </awards>
    </member>
    <member>
      <name>username2</name>
      <team id="1200">team1 name</team>
      <rank>
        <today rank="46" points="577.80" results="178"/>
        <yesterday rank="45" points="580.00" results="178"/>
        <lastweek rank="40" points="586.60" results="178"/>
        <lastmonth rank="75" points="503.60" results="169"/>
      </rank>
      <awards>
        <global gold="0" silver="0" bronze="0" medal="0"/>
        <local gold="0" silver="2" bronze="2" medal="6"/>
      </awards>
    </member>
  </members>
</sitemembers>

以及所需的输出格式

    Members ranking for team1 name (teamid: 1200)

    1. username
    Current rank: 34 : 613.30 pts (submissions: 70)
    Yesterday rank: 33 : 614.20 ps (submissions: 70)
    Last week rank: 60 : 541.70 pts (submissions: 68)
    Last month rank: 163 : 415.60 (submissions: 63)
    Global awards: 1 medals - [Gold: 0 Silver: 0 Bronze: 0]
    Local awards: 3 medals - [Gold: 5 Silver: 2 Bronze: 0]

    2. username2
    Current rank: 46 : 577.80 pts (submissions: 178)
    Yesterday rank: 45 : 580.00 ps (submissions: 178)
    Last week rank: 40 : 588.60 pts (submissions: 178)
    Last month rank: 75 : 503.60 (submissions: 169)
    Global awards: 0 medals - [Gold: 0 Silver: 0 Bronze: 0]
    Local awards: 6 medals - [Gold: 0 Silver: 2 Bronze: 2]

任何帮助都可以引导我走向正确的方向。谢谢

Hi guys first post on this site so excuse the formatting.

I'm new to xml parsing with php and well this xml feed has me stumped. I get using purely element based formats, but attributes this complex not so much. I tried to get the site to change to easier element based format but they're sticking with this format using attributes and I'm having a hard time pulling all the data in a listing/ranking style format particularly for each members' rankings and points.

example of xml file with only 2 members, but in reality there could 100s and 1000s depending on team id.

<sitemembers>
  <members>
    <member>
      <name>username</name>
      <team id="1200">team1 name</team>
      <rank>
        <today rank="34" points="613.30" results="70"/>
        <yesterday rank="33" points="614.20" results="70"/>
        <lastweek rank="60" points="541.70" results="68"/>
        <lastmonth rank="163" points="415.60" results="63"/>
      </rank>
      <awards>
        <global gold="0" silver="0" bronze="0" medal="1"/>
        <local gold="5" silver="2" bronze="0" medal="3"/>
      </awards>
    </member>
    <member>
      <name>username2</name>
      <team id="1200">team1 name</team>
      <rank>
        <today rank="46" points="577.80" results="178"/>
        <yesterday rank="45" points="580.00" results="178"/>
        <lastweek rank="40" points="586.60" results="178"/>
        <lastmonth rank="75" points="503.60" results="169"/>
      </rank>
      <awards>
        <global gold="0" silver="0" bronze="0" medal="0"/>
        <local gold="0" silver="2" bronze="2" medal="6"/>
      </awards>
    </member>
  </members>
</sitemembers>

and the output wanted in format

    Members ranking for team1 name (teamid: 1200)

    1. username
    Current rank: 34 : 613.30 pts (submissions: 70)
    Yesterday rank: 33 : 614.20 ps (submissions: 70)
    Last week rank: 60 : 541.70 pts (submissions: 68)
    Last month rank: 163 : 415.60 (submissions: 63)
    Global awards: 1 medals - [Gold: 0 Silver: 0 Bronze: 0]
    Local awards: 3 medals - [Gold: 5 Silver: 2 Bronze: 0]

    2. username2
    Current rank: 46 : 577.80 pts (submissions: 178)
    Yesterday rank: 45 : 580.00 ps (submissions: 178)
    Last week rank: 40 : 588.60 pts (submissions: 178)
    Last month rank: 75 : 503.60 (submissions: 169)
    Global awards: 0 medals - [Gold: 0 Silver: 0 Bronze: 0]
    Local awards: 6 medals - [Gold: 0 Silver: 2 Bronze: 2]

Any help is appreciated in steering me in the right direction. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

薆情海 2024-08-29 00:57:53

使用 SimpleXML

$sitemembers = simplexml_load_string($xml);

echo '<ol>';
foreach ($sitemembers->members->member as $member)
{
    $today = $member->rank->today;
    echo '<li>', $member->name, '<br />',
         'Current rank: ', $today['rank'], ' : ', $today['points'];
    // etc...
    echo '</li>';
}
echo '</ol>';

Use SimpleXML

$sitemembers = simplexml_load_string($xml);

echo '<ol>';
foreach ($sitemembers->members->member as $member)
{
    $today = $member->rank->today;
    echo '<li>', $member->name, '<br />',
         'Current rank: ', $today['rank'], ' : ', $today['points'];
    // etc...
    echo '</li>';
}
echo '</ol>';
乙白 2024-08-29 00:57:53

将您的 xml 代码放入 local.xml 中

$sitemembers = simplexml_load_file("local.xml");

foreach ($sitemembers->members->member as $member)
{


$today = $member->rank->today;
$yesterday=$member->rank->yesterday;
$lastweek=$member->rank->lastweek;
$lastmonth=$member->rank->lastweek;
$awards1=$member->awards->global;
$awards2=$member->awards->local;

echo '<li>', $member->name, '<br />',
     'Current rank: ', $today['rank'], ' : ', $today['points'], 'pts' , '(Submission :', $today['results'], ')';
echo '<br />',
     'Yesterday rank: ', $yesterday['rank'], ' : ', $yesterday['points'], 'pts' , '(Submission :', $yesterday['results'], ')';
echo '<br />',
     'Last week rank: ', $lastweek['rank'], ' : ', $lastweek['points'], 'pts' , '(Submission :', $lastweek['results'], ')';
echo '<br />',
     'Last month rank: ', $lastmonth['rank'], ' : ', $lastmonth['points'], 'pts' , '(Submission :', $lastmonth['results'], ')';
echo '<br />',
     'Global award: ', $awards1['medal'], ' - '.'[ Gold:'.$awards1['gold'].' '.'Silver:'.$awards1['silver'].' '.'Bronze:'.$awards1['bronze'].']';
 echo '<br />',    
     'Local award: ', $awards2['medal'], ' - '.'[ Gold:'.$awards2['gold'].' '.'Silver:'.$awards2['silver'].' '.'Bronze:'.$awards2['bronze'].']';
echo '</li>';

}
?>

Place your xml code in local.xml

$sitemembers = simplexml_load_file("local.xml");

foreach ($sitemembers->members->member as $member)
{


$today = $member->rank->today;
$yesterday=$member->rank->yesterday;
$lastweek=$member->rank->lastweek;
$lastmonth=$member->rank->lastweek;
$awards1=$member->awards->global;
$awards2=$member->awards->local;

echo '<li>', $member->name, '<br />',
     'Current rank: ', $today['rank'], ' : ', $today['points'], 'pts' , '(Submission :', $today['results'], ')';
echo '<br />',
     'Yesterday rank: ', $yesterday['rank'], ' : ', $yesterday['points'], 'pts' , '(Submission :', $yesterday['results'], ')';
echo '<br />',
     'Last week rank: ', $lastweek['rank'], ' : ', $lastweek['points'], 'pts' , '(Submission :', $lastweek['results'], ')';
echo '<br />',
     'Last month rank: ', $lastmonth['rank'], ' : ', $lastmonth['points'], 'pts' , '(Submission :', $lastmonth['results'], ')';
echo '<br />',
     'Global award: ', $awards1['medal'], ' - '.'[ Gold:'.$awards1['gold'].' '.'Silver:'.$awards1['silver'].' '.'Bronze:'.$awards1['bronze'].']';
 echo '<br />',    
     'Local award: ', $awards2['medal'], ' - '.'[ Gold:'.$awards2['gold'].' '.'Silver:'.$awards2['silver'].' '.'Bronze:'.$awards2['bronze'].']';
echo '</li>';

}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文