I need to restructure a very large xml source, example is at
我需要重构一个非常大的xml源代码,例如
http://www.fluffyduck.com.au/sampleXML.xml
http://www.fluffyduck.com.au/sampleXML.xml
I need to modify it for jstree however I'm not sure how to manipulate the data recursively, as loading it as xml with simpleXml only see's the first 1 user record.
我需要为jstree修改它,但是我不确定如何递归操作数据,因为将它加载为xml with simpleXml只看到前1个用户记录。
<user id="41" username="bsmain" firstname="Boss" lastname="MyTest" fullname="Test Name" email="lalal@test.com" logins="1964" lastseen="11/09/2012">
</user>
to
<user id="41">
<content><name>bsmain</name></content>
</user>
The problem is some xml lines do not have a closing tag such as , but instead look like this :
问题是一些xml行没有结束标记,但是看起来像这样:
<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal@test.com" logins="4" lastseen="08/09/2009"/>
If i modify this record and add underling jstree does not recognise it, i'm presuming the /> at the end is the same as ?
如果我修改此记录并添加underling jstree无法识别它,我假设最后的/>是相同的?
I did want to do this in XML but am thinking it may be easier, to simply somehow parse the xml file 'line by line', read in the line of data explode it perhaps, then create a new variable storing it with modified contents such as :
我确实想用XML做这个,但我觉得它可能更容易,只是以某种方式逐行解析xml文件,读取数据行可能会爆炸它,然后创建一个新的变量存储它与修改的内容,如如:
<user id="61">
<content><name>bsmain</name>
</user>
and on the rows where /> exists at the end, manually insert a tag.
并且在最后存在/>的行上,手动插入标记。
there has to be a smarter/faster way to achieve this.
必须有一个更聪明/更快的方法来实现这一目标。
1 个解决方案
#1
1
Your best bet is to use DOMDocument
for XML parsing. I have written an example that transforms attributes (excluding the id
attribute) to content elements:
最好的办法是使用DOMDocument进行XML解析。我编写了一个将属性(不包括id属性)转换为内容元素的示例:
Code
码
<?php
$s =
'<users>' .
'<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal@test.com" logins="4" lastseen="08/09/2009"/>' .
'<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal@test.com" logins="4" lastseen="08/09/2009"/>' .
'<user id="8" test="testvalue"></user>' .
'</users>';
$doc = new DOMDocument();
$doc->loadXML($s);
$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))
{
$content->appendChild($doc->createElement($attr->name, $attr->value));
$i--;
}
}
}
}
}
header("Content-Type: text/xml");
echo $doc->saveXML();
?>
Output
产量
<users>
<user id="61">
<content>
<username>underling</username>
<firstname>Under</firstname>
<lastname>MyTest</lastname>
<fullname>Test Name</fullname>
<email>lalal@test.com</email>
<logins>4</logins>
<lastseen>08/09/2009</lastseen>
</content>
</user>
<user id="61">
<content>
<username>underling</username>
<firstname>Under</firstname>
<lastname>MyTest</lastname>
<fullname>Test Name</fullname>
<email>lalal@test.com</email>
<logins>4</logins>
<lastseen>08/09/2009</lastseen>
</content>
</user>
<user id="8">
<content>
<test>testvalue</test>
</content>
</user>
</users>
#1
1
Your best bet is to use DOMDocument
for XML parsing. I have written an example that transforms attributes (excluding the id
attribute) to content elements:
最好的办法是使用DOMDocument进行XML解析。我编写了一个将属性(不包括id属性)转换为内容元素的示例:
Code
码
<?php
$s =
'<users>' .
'<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal@test.com" logins="4" lastseen="08/09/2009"/>' .
'<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal@test.com" logins="4" lastseen="08/09/2009"/>' .
'<user id="8" test="testvalue"></user>' .
'</users>';
$doc = new DOMDocument();
$doc->loadXML($s);
$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))
{
$content->appendChild($doc->createElement($attr->name, $attr->value));
$i--;
}
}
}
}
}
header("Content-Type: text/xml");
echo $doc->saveXML();
?>
Output
产量
<users>
<user id="61">
<content>
<username>underling</username>
<firstname>Under</firstname>
<lastname>MyTest</lastname>
<fullname>Test Name</fullname>
<email>lalal@test.com</email>
<logins>4</logins>
<lastseen>08/09/2009</lastseen>
</content>
</user>
<user id="61">
<content>
<username>underling</username>
<firstname>Under</firstname>
<lastname>MyTest</lastname>
<fullname>Test Name</fullname>
<email>lalal@test.com</email>
<logins>4</logins>
<lastseen>08/09/2009</lastseen>
</content>
</user>
<user id="8">
<content>
<test>testvalue</test>
</content>
</user>
</users>