1

私は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">の種は、アルペジオという名前の子とその子の構文解析です。私の脳はそこに到達するためのループを見つけて誤動作しています。もあるとどうなり<discussion id="3">ますか?

また、現在のphpスクリプトはnoob用であると感じており、簡単な方法があると感じています。私が本当にサンプルplsが必要な人を助けてください:/

4

2 に答える 2

4

XPathを使用して、XMLで必要なものを見つけてみませんか?

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

于 2012-08-03T03:20:44.230 に答える
2
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')");


}
于 2012-08-06T12:39:09.353 に答える