在SimpleXML中,如何添加现有的SimpleXMLElement作为子元素? [英] In SimpleXML, how can I add an existing SimpleXMLElement as a child element?

查看:198
本文介绍了在SimpleXML中,如何添加现有的SimpleXMLElement作为子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SimpleXMLElement对象$ child和一个SimpleXMLElement对象$ parent.

I have a SimpleXMLElement object $child, and a SimpleXMLElement object $parent.

如何将$ child作为$ parent的子代添加?有什么方法可以做到,而无需转换为DOM并返回吗?

How can I add $child as a child of $parent? Is there any way of doing this without converting to DOM and back?

addChild()方法似乎只允许我创建一个新的空元素,但是当我要添加$ child的元素也有子元素时,这无济于事.我在想可能需要递归.

The addChild() method only seems to allow me to create a new, empty element, but that doesn't help when the element I want to add $child also has children. I'm thinking I might need recursion here.

推荐答案

我知道这不是最有用的答案,但是特别是由于您正在创建/修改XML时,我将切换到使用DOM函数. SimpleXML对于访问简单文档有好处,但在更改它们方面却很差.

I know this isn't the most helpful answer, but especially since you're creating/modifying XML, I'd switch over to using the DOM functions. SimpleXML's good for accessing simple documents, but pretty poor at changing them.

如果SimpleXML在所有其他地方都对您友好,并且您想坚持使用它,那么您仍然可以选择暂时跳至DOM函数以执行所需的操作,然后使用 dom_import_simplexml()

If SimpleXML is treating you kindly in all other places and you want to stick with it, you still have the option of jumping over to the DOM functions temporarily to perform what you need to and then jump back again, using dom_import_simplexml() and simplexml_import_dom(). I'm not sure how efficient this is, but it might help you out.

这篇关于在SimpleXML中,如何添加现有的SimpleXMLElement作为子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆