XML基础到动态php页面

时间:2014-02-04 15:25:38

标签: php xml

我想了解一些关于创建一个php页面的信息,其中包含来自“todolist progam”的generate xml中的一些信息。

以下是生成的XML:

<TODOLIST PROJECTNAME="" FILEFORMAT="9" EARLIESTDUEDATE="0.00000000" LASTMOD="41674.63037037" LASTMODSTRING="04/02/2014 15:07" NEXTUNIQUEID="4" FILENAME="tasks.xml" FILEVERSION="2" APPVER="6.8.8.0">

问题是,它会生成一个<TASK TITLE (ETC) />而不是<TASK> <TITLE></TASK> </TITLE>

的XML

并且无法找到使用这种形式的XML编写的解决方案。我在w3schools.com上找到了一个脚本,但XML的生成方式不同。

我想知道该怎么办?

感谢! :)

1 个答案:

答案 0 :(得分:0)

本手册对您有很大好处:http://www.php.net/manual/en/simplexml.examples-basic.php

示例XML:

<tasks>
    <task id="1" title="feed the dog" status="0" owner="Tom" />
    <task id="2" title="learn simplexml" status="10" owner="user3271431"/>
</tasks>

任务节点具有属性,例如身份,头衔,身份等。

创建一个simplexml-object:

$xml = simplexml_load_string($x); // assuming the XML is in $x

回应所有任务:

foreach ($xml->task as $task)
    echo $task['title'] . "(" . $task['id'] . ")" . PHP_EOL;

**使用owner = Tom选择并回显任务:

$tomstasks = $xml->xpath("/tasks/task[@owner = 'Tom']");

foreach ($tomstasks as $task)
    echo "Toms task: " . $task['title'] . PHP_EOL;

看到它有效:https://eval.in/97975