dsdzvprlp51692469 2011-08-03 04:37
浏览 49

如何创建XML元素的副本,但更改元素的名称并删除原始元素? [重复]

Possible Duplicate:
How do you rename a tag in SimpleXML through a DOM object?

If I have an XML document like this:

<document>
  <dogs>
    <bulldog>Blu</bulldog>
    <terrier>Benjie</terrier>
  </dogs>
  <cats>
    <tiger>Tiggger</tiger>
    <lion>Cowardly</lion>
  </cats>
</document>

And I want to reprint it, adding some attributes, but not keep the original....

<document>
  <canine type="fiction">
    <bulldog>Blu</bulldog>
    <terrier>Benjie</terrier>
  </canine>
  <feline type="fiction">
    <tiger>Tiggger</tiger>
    <lion>Cowardly</lion>
  </feline>
</document>

What strategy would I use, to do so in SimpleXML?

  • 写回答

1条回答 默认 最新

  • douyan8070 2011-08-03 05:11
    关注

    I think you will need to make a deep copy of the data, erase the <dogs> element, create a new <canine> element, and then append the deep copy from earlier.

    Edit: While you can make clones with $copy = clone $sxml->dogs; and then do unset($sxml->dogs); , the actual recursive-adding is a pain and you may need to code your own recursive stuff. There are some examples in the PHP.net comments.

    If things get more complex, you might also want to consider DOM instead. (You can take SimpleXML and convert to and from DOM if necessary.)

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程