由于 StackOverflow 社区的帮助,我已经取得了一些成功,以修改复杂的 XML 源以与 jsTree 一起使用。但是,既然我有可用的数据,只有在我手动编辑 XML 以执行以下操作时才如此:
- 将所有
<user>
标签重命名为<item>
- 删除第一个
<user>
标签之前的一些元素 'encoding=UTF-8'
在 XML 开启器中插入一个- 最后将
<response>
(打开 XML 标记)修改为<root>
XML 文件示例:SampleXML
我在这里和谷歌上阅读了很多页面,但找不到实现上述项目的方法。
第 (2) 点我发现通过SimpleXML加载它并使用UNSET
i 可以删除我不需要的部分,但是我仍然遇到其他问题。
我想我也许可以使用SimpleXML(我更熟悉)修改源代码,然后通过我之前提供的帮助继续修改代码。
<?php
$s = file_get_contents('http://www.fluffyduck.com.au/sampleXML.xml');
$doc1 = simplexml_load_string($s);
unset($doc1->row);
unset($doc1->display);
#$moo = $doc1->user;
echo '<textarea>';
echo $doc1->asXML();
echo '</textarea>';
$doc = new DOMDocument();
$doc->loadXML($doc1);
$users = $doc->getElementsByTagName("user");
foreach ($users as $user)
{
if ($user->hasAttributes())
{
// create content node
$content = $user->appendChild($doc->createElement("content"));
// transform attributes into content elements
for ($i = 0; $i < $user->attributes->length; $i++)
{
$attr = $user->attributes->item($i);
if (strtolower($attr->name) != "id")
{
if ($user->removeAttribute($attr->name))
{
if($attr->name == "username") {
$content->appendChild($doc->createElement('name', $attr->value));
} else {
$content->appendChild($doc->createElement($attr->name, $attr->value));
}
$i--;
}
}
}
}
}
$doc->saveXML();
header("Content-Type: text/xml");
echo $doc->saveXML();
?>