大家好我正在寻求帮助从PHP文件中获取文本。 我是PHP和XML的初学者,无论如何都开始喜欢它。
这是我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<activity id="1" moduleid="3" modulename="lesson" contextid="22">
<lesson id="1">
<course>2</course>
<name>asdasdasasdasda</name>
<practice>0</practice>
<modattempts>0</modattempts>
<usepassword>0</usepassword>
<password></password>
<dependency>0</dependency>
<conditions>O:8:"stdClass":3:{s:9:"timespent";i:0;s:9:"completed";i:0;s:15:"gradebetterthan";i:0;}</conditions>
<grade>100</grade>
<custom>1</custom>
<ongoing>0</ongoing>
<usemaxgrade>0</usemaxgrade>
<maxanswers>4</maxanswers>
<maxattempts>1</maxattempts>
<review>0</review>
<nextpagedefault>0</nextpagedefault>
<feedback>0</feedback>
<minquestions>0</minquestions>
<maxpages>0</maxpages>
<timed>0</timed>
<maxtime>20</maxtime>
<retake>0</retake>
<activitylink>0</activitylink>
<mediafile>/airline ticket system.txt</mediafile>
<mediaheight>480</mediaheight>
<mediawidth>640</mediawidth>
<mediaclose>0</mediaclose>
<slideshow>0</slideshow>
<width>640</width>
<height>480</height>
<bgcolor>#FFFFFF</bgcolor>
<displayleft>0</displayleft>
<displayleftif>0</displayleftif>
<progressbar>0</progressbar>
<showhighscores>0</showhighscores>
<maxhighscores>10</maxhighscores>
<available>0</available>
<deadline>0</deadline>
<timemodified>1342762739</timemodified>
<pages>
</pages>
<grades>
</grades>
<highscores>
</highscores>
<timers>
</timers>
</lesson>
</activity>
这是我目前的PHP脚本:
<?php
$data = simplexml_load_file('lesson.xml');
foreach ($data as $dxdata) {
echo "Activity ID: ".$data->activity[0]['id']."<br />";
echo "Module ID: ".$data->activity['moduleid']."<br />";
echo "Module Name: ".$data->activity['modulename']."<br />";
echo "Context ID: ".$data->activity['contextid']."<br />";
echo "Lessons ID: ".$data->lesson[0]['id']."<br />";
echo "Course: ".$data->lesson[0]->course."<br />";
echo "Name: ".$data->lesson[0]->name."<br />";
echo "Practice: ".$data->lesson[0]->practice."<br />";
echo "Modattemps: ".$data->lesson[0]->modattempts."<br />";
echo "usepassword ".$data->lesson[0]->usepassword."<br />";
}
?>
这是我的PHP脚本的输出:
Activity ID:
Module ID:
Module Name:
Context ID:
Lessons ID: 1
Course: 2
Name: asdasdasasdasda
Practice: 0
Modattemps: 0
usepassword 0
您看到我的脚本输出无法获取活动ID,模块ID和模块名称。那些是我从xml中获取困难的那些。
我一直在网上搜索这个已经找不到具体的例子来帮我解决这个问题。问这里是我的最后一个选择。我将感谢所有提供给我的帮助。
答案 0 :(得分:1)
下面是一个获取XML并输出以下内容的示例:
<activity>
属性的详细信息(例如id="1"
)<lesson>
的课程详细信息(示例中只有一个)请注意,我已将$data
重命名为$activity
,以便更好地将XML元素名称与PHP变量匹配,并与$lesson
相同。
$activity = simplexml_load_file('lesson.xml');
// Access attributes with array-style syntax
echo "Activity ID: ".$activity['id']."<br />";
echo "Module ID: ".$activity['moduleid']."<br />";
echo "Module Name: ".$activity['modulename']."<br />";
echo "Context ID: ".$activity['contextid']."<br />";
// Loop over each <lesson> element directly under <activity>
foreach ($activity->lesson as $lesson) {
echo "Lessons ID: ".$lesson['id']."<br />";
echo "Course: ".$lesson->course."<br />";
echo "Name: ".$lesson->name."<br />";
echo "Practice: ".$lesson->practice."<br />";
echo "Modattemps: ".$lesson->modattempts."<br />";
echo "usepassword ".$lesson->usepassword."<br />";
}
这输出以下内容:
Activity ID: 1
Module ID: 3
Module Name: lesson
Context ID: 22
Lessons ID: 1
Course: 2
Name: asdasdasasdasda
Practice: 0
Modattemps: 0
usepassword 0
有关使用SimpleXML的更多详细信息,请参阅Basic SimpleXML usage PHP手册页。