首先让我告诉自己我是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><p>Topic Major Scalesssss</p></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><p>We will learn all about Arpegios</p></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目前的脚本是针对菜鸟的,并且感觉有一种简单的方法。帮助我们,我真的需要一个样品:/
答案 0 :(得分:4)
为什么不使用XPath在XML中找到你需要的东西?
答案 1 :(得分: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')");
}