Reputation: 179
let me begin with telling myself I am a beginner with PHP. Anyway I am having a struggle with it right now parsing xml, lots of xml. Hours and hours already I tried solving it and its already giving me a headache. I am sure for the pros this is like picking a booger in your nose.
I will begin with the 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>
And this is currently I have with 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();
?>
and this is my output guys
Topic
ID: 1
Name: Major Scale Topic
Description:
Topic Major Scalesssss
Order: 1
Created: 1343897212
Modified: 1343897644
Everything is looking good yeah? The headache part I'm getting is the parsing of <discussion id="2">
and its child, the one with the name Arpeggios. My brain is malfunctioning figuring out a loop to get there. What if there's also <discussion id="3">
?
I am also getting a feeling that my php currently script is for noob, and getting a feel there is an easy way for it. Help me guys I really need a sample pls :/
Upvotes: 1
Views: 4305
Reputation: 179
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')");
}
Upvotes: 2
Reputation: 1013
Why not use XPath to find what you need in the XML?
http://php.net/manual/en/simplexmlelement.xpath.php
Upvotes: 4