用PHP解析xml中的嵌套子项


Parsing nested child in xml with PHP

让我首先告诉自己,我是PHP的初学者。无论如何,我现在很难解析xml,很多xml。我已经试了好几个小时了,它已经让我头疼了。我相信,对于职业选手来说,这就像是在你的鼻子里挑了一个混蛋。

我将从xml:开始

<?xml version="1.0" encoding="UTF-8"?>
<activity id="5" moduleid="13" modulename="forum" contextid="37">
  <forum id="5">
    <type>news</type>
    <name>News forum</name>
    <intro>General news and announcements</intro>
    <introformat>0</introformat>
    <assessed>0</assessed>
    <assesstimestart>0</assesstimestart>
    <assesstimefinish>0</assesstimefinish>
    <scale>0</scale>
    <maxbytes>0</maxbytes>
    <maxattachments>1</maxattachments>
    <forcesubscribe>1</forcesubscribe>
    <trackingtype>1</trackingtype>
    <rsstype>0</rsstype>
    <rssarticles>0</rssarticles>
    <timemodified>1343889253</timemodified>
    <warnafter>0</warnafter>
    <blockafter>0</blockafter>
    <blockperiod>0</blockperiod>
    <completiondiscussions>0</completiondiscussions>
    <completionreplies>0</completionreplies>
    <completionposts>0</completionposts>
    <discussions>
      <discussion id="1">
        <name>Major Scale Topic</name>
        <firstpost>1</firstpost>
        <userid>2</userid>
        <groupid>-1</groupid>
        <assessed>1</assessed>
        <timemodified>1343897644</timemodified>
        <usermodified>2</usermodified>
        <timestart>0</timestart>
        <timeend>0</timeend>
        <posts>
          <post id="1">
            <parent>0</parent>
            <userid>2</userid>
            <created>1343897212</created>
            <modified>1343897644</modified>
            <mailed>0</mailed>
            <subject>Major Scale Topic</subject>
            <message>&lt;p&gt;Topic Major Scalesssss&lt;/p&gt;</message>
            <messageformat>1</messageformat>
            <messagetrust>0</messagetrust>
            <attachment>1</attachment>
            <totalscore>0</totalscore>
            <mailnow>0</mailnow>
            <ratings>
            </ratings>
          </post>
        </posts>
      </discussion>
      <discussion id="2">
        <name>Arpeggios</name>
        <firstpost>2</firstpost>
        <userid>2</userid>
        <groupid>-1</groupid>
        <assessed>1</assessed>
        <timemodified>1343915550</timemodified>
        <usermodified>2</usermodified>
        <timestart>0</timestart>
        <timeend>0</timeend>
        <posts>
          <post id="2">
            <parent>0</parent>
            <userid>2</userid>
            <created>1343915550</created>
            <modified>1343915550</modified>
            <mailed>0</mailed>
            <subject>Arpeggios</subject>
            <message>&lt;p&gt;We will learn all about Arpegios&lt;/p&gt;</message>
            <messageformat>1</messageformat>
            <messagetrust>0</messagetrust>
            <attachment></attachment>
            <totalscore>0</totalscore>
            <mailnow>0</mailnow>
            <ratings>
            </ratings>
          </post>
        </posts>
      </discussion>
    </discussions>
    <subscriptions>
    </subscriptions>
    <readposts>
    </readposts>
    <trackedprefs>
    </trackedprefs>
  </forum>
</activity>

这是我目前使用的PHP:

<?php
function getTopic()
{
echo "<h1>Topic</h1>";
$forumxml = simplexml_load_file('forum.xml');
global $id;
$id = $forumxml->forum->discussions->discussion['id'];
$name = $forumxml->forum->discussions->discussion->name;
$description = $forumxml->forum->discussions->discussion->posts->post->message;
$order = $forumxml->forum->discussions->discussion->posts->post['id'];
$created = $forumxml->forum->discussions->discussion->posts->post->created;
$modified = $forumxml->forum->discussions->discussion->posts->post->modified;
echo "ID: ". $id."<br />";
echo "Name: ".$name."<br />";
echo "Description: ".$description."<br />";
echo "Order: ".$order."<br />";
echo "Created: ".$created."<br />";
echo "Modified: ".$modified."<br />";
}
getTopic();
?>

这是我的输出人员

Topic
ID: 1
Name: Major Scale Topic
Description:
Topic Major Scalesssss
Order: 1
Created: 1343897212
Modified: 1343897644

一切看起来都很好,是吗?我遇到的最头疼的部分是<discussion id="2">及其子项的解析,即名为Arpeggios的子项。我的大脑出了故障,无法找到一个到达那里的环路。如果还有<discussion id="3">呢?

我也有一种感觉,我的php当前脚本是为noob编写的,并感觉有一种简单的方法。帮助我,我真的需要一个样本,请:/

为什么不使用XPath在XML中查找所需内容?

http://php.net/manual/en/simplexmlelement.xpath.php

function getTopic($csID, $ID, $userID)
{
echo "<h1>Topic</h1>";
$forumxml = simplexml_load_file('forum.xml');
global $id;
foreach ($forumxml->forum->discussions->discussion as $discussion) {
   $id = $discussion->attributes()->id;
   $name = $discussion->name.'<br />';
   $description = $discussion->posts->post->message;
   $order = $discussion->posts->post['id'];
   $created = $discussion->posts->post->created;
   $modified = $discussion->posts->post->modified;
echo "ID: ". $id."<br />";
echo "Classroom ID: ".$csID."<br />";
echo "Course ID: ".$ID."<br />";
echo "Name: ".$name."<br />";
echo "Description: ".$description."<br />";
echo "Order: ".$order."<br />";
echo "User ID: ".$userID."<br />";
echo "Created: ".$created."<br />";
echo "Modified: ".$modified."<br />";
echo "<br />";
}
mysql_select_db("project");
mysql_query("INSERT INTO topics VALUES ('$id','$csID','$ID','$name','$description','$order','$userID','$created','$modified')");

}