我正在尝试构建一个相当复杂的XML文档.
我有一堆重复的XML文档部分.我以为我会使用多个字符串模板作为节的基础文档,并使用simplexml_load_string创建XML元素的实例.
所以我有一个SimpleXMLElement实例作为基础文档
$ root = simplexml_load_string($ template_root);
然后我遍历我的数据库中的一些项目,创建新的SimpleXMLElement,如下所示:
for(bla bla bla):
$ item = simplexml_load_string($ template_item); //用项目做的东西//尝试将项目添加到根文档中
//被困在这里..不能做$ root-> items-> addChild($ item)ENDFOR;
我不能调用addChild,因为它只需要一个标签名称和值..你不能addChild另一个SimpleXMLElement.
我在这里错过了什么吗?看起来真的很蠢,addChild不能将SimpleXMLELement作为参数.
有没有其他方法可以做到这一点?(除了使用不同的xml库)
Art*_*cto 57
据我所知,你不能用SimpleXML做到这一点,因为addChild不会对元素进行深层复制(通过调用可以很容易地克服指定标记名称所必需的SimpleXMLElement::getName()).
一种解决方案是使用DOM代替:
有了这个功能:
function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
$toDom = dom_import_simplexml($to);
$fromDom = dom_import_simplexml($from);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
Run Code Online (Sandbox Code Playgroud)
我们有
<?php
header("Content-type: text/plain");
$sxml = simplexml_load_string("<root></root>");
$n1 = simplexml_load_string("<child>one</child>");
$n2 = simplexml_load_string("<child><k>two</k></child>");
sxml_append($sxml, $n1);
sxml_append($sxml, $n2);
echo $sxml->asXML();
Run Code Online (Sandbox Code Playgroud)
输出
<?xml version="1.0"?>
<root><child>one</child><child><k>two</k></child></root>
Run Code Online (Sandbox Code Playgroud)
另请参阅一些使用递归函数的用户注释addChild,例如这个.
小智 16
您可以使用此函数,该函数基于从源创建具有属性的子项:
function xml_adopt($root, $new) {
$node = $root->addChild($new->getName(), (string) $new);
foreach($new->attributes() as $attr => $value) {
$node->addAttribute($attr, $value);
}
foreach($new->children() as $ch) {
xml_adopt($node, $ch);
}
}
$xml = new SimpleXMLElement("<root/>");
$child = new SimpleXMLElement("<content><p a=\"aaaaaaa\">a paragraph</p><p>another <br/>p</p></content>");
xml_adopt($xml, $child);
echo $xml->asXML()."\n";
Run Code Online (Sandbox Code Playgroud)
这将产生:
<?xml version="1.0"?>
<root><content><p a="aaaaaaa">a paragraph</p><p>another p<br/></p></content></root>
Run Code Online (Sandbox Code Playgroud)
xml_adopt()示例不保留命名空间节点.
我的编辑被拒绝了,因为它改变了很多?垃圾邮件?
这是一个保留名称空间的xml_adopt()版本.
function xml_adopt($root, $new, $namespace = null) {
// first add the new node
// NOTE: addChild does NOT escape "&" ampersands in (string)$new !!!
// replace them or use htmlspecialchars(). see addchild docs comments.
$node = $root->addChild($new->getName(), (string) $new, $namespace);
// add any attributes for the new node
foreach($new->attributes() as $attr => $value) {
$node->addAttribute($attr, $value);
}
// get all namespaces, include a blank one
$namespaces = array_merge(array(null), $new->getNameSpaces(true));
// add any child nodes, including optional namespace
foreach($namespaces as $space) {
foreach ($new->children($space) as $child) {
xml_adopt($node, $child, $space);
}
}
}
Run Code Online (Sandbox Code Playgroud)
(编辑:示例已添加)
$xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel></channel></rss>');
$item = new SimpleXMLElement(
'<item xmlns:media="http://search.yahoo.com/mrss/">
<title>Slide Title</title>
<description>Some description</description>
<link>http://example.com/img/image.jpg</link>
<guid isPermaLink="false">A1234</guid>
<media:content url="http://example.com/img/image.jpg" medium="image" duration="15">
</media:content>
</item>');
$channel = $xml->channel;
xml_adopt($channel, $item);
// output:
// Note that the namespace is (correctly) only preserved on the root element
'<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<item>
<title>Slide Title</title>
<description>Some description</description>
<link>http://example.com/img/image.jpg</link>
<guid isPermaLink="false">A1234</guid>
<media:content url="http://example.com/img/image.jpg" medium="image" duration="15">
</media:content>
</item>
</channel>
</rss>'
Run Code Online (Sandbox Code Playgroud)