如何在 xml 中将子节点移动到父节点级别 ?

发布于 2024-10-04 04:08:12 字数 966 浏览 4 评论 0原文

最近在做xml编码,出现了问题。

给定这样的 xml:

<Colonel id="1">
  <Lieutenant_Colonel id="2">
    <Secretary id="6"/>
  </Lieutenant_Colonel>
  <Lieutenant_Colonel id="3">
    <Secretary id="7"/>
  </Lieutenant_Colonel>
  <Secretary id="5"/>
</Colonel>

现在上校(id=1)已经和秘书(id=5)一起走了,

我们想要的 xml 类似于

<!-- <Colonel id="1"> -->
  <Lieutenant_Colonel id="2">
    <Secretary id="6"/>
  </Lieutenant_Colonel>
  <Lieutenant_Colonel id="3">
    <Secretary id="7"/>
  </Lieutenant_Colonel>
  <!--
  <Secretary id="5"/>
</Colonel> -->

<Lieutenant_Colonel id="2">
  <Secretary id="6"/>
</Lieutenant_Colonel>
<Lieutenant_Colonel id="3">
  <Secretary id="7"/>
</Lieutenant_Colonel>

如何做这项工作?

请帮助我

Recently I have been working with the xml coding,and the problem happened.

Given the xml like this:

<Colonel id="1">
  <Lieutenant_Colonel id="2">
    <Secretary id="6"/>
  </Lieutenant_Colonel>
  <Lieutenant_Colonel id="3">
    <Secretary id="7"/>
  </Lieutenant_Colonel>
  <Secretary id="5"/>
</Colonel>

now the Colonel(id=1) has gone with the Secretary(id=5)

the xml we want is like

<!-- <Colonel id="1"> -->
  <Lieutenant_Colonel id="2">
    <Secretary id="6"/>
  </Lieutenant_Colonel>
  <Lieutenant_Colonel id="3">
    <Secretary id="7"/>
  </Lieutenant_Colonel>
  <!--
  <Secretary id="5"/>
</Colonel> -->

or

<Lieutenant_Colonel id="2">
  <Secretary id="6"/>
</Lieutenant_Colonel>
<Lieutenant_Colonel id="3">
  <Secretary id="7"/>
</Lieutenant_Colonel>

How to do this work?

please help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-10-11 04:08:12

这是一个相当长的问题,所以我必须提及其他问题。首先,考虑这个函数:

function RemoveColonel($id, $secretaryId)
{
    $colXpath = '//colonel[@id="' . $id . '"]';
    $secXpath = $colXpath . '/Secretary[@id="' . $secretaryId . '"]';

    RemoveNode($secXpath);
    $colChildren = $rootXml->xpath($colXpath)->children();
    $children = array();

    foreach ($colChildren as $child ){
        $children[] = $child;
    }

    RemoveNode($colXpath);
    return $children;
}

给定上校的 ID 和该上校的秘书,它删除秘书,然后保存上校的孩子以删除上校并返回孩子。之后,如果愿意,您可以再次将元素插入根节点。如果在删除上校时无法获得秘书 ID,则可以解决 xpath 问题,通过删除 id 规范来删除作为上校子级的秘书。

RemoveNode 代码有点长,所以我必须给你 这个关于如何在 PHP 中使用 SimpleXML 删除节点的问题。

我希望我能提供一些帮助!

This is a pretty long them, so I´ll have to refer other questions. First, consider this function:

function RemoveColonel($id, $secretaryId)
{
    $colXpath = '//colonel[@id="' . $id . '"]';
    $secXpath = $colXpath . '/Secretary[@id="' . $secretaryId . '"]';

    RemoveNode($secXpath);
    $colChildren = $rootXml->xpath($colXpath)->children();
    $children = array();

    foreach ($colChildren as $child ){
        $children[] = $child;
    }

    RemoveNode($colXpath);
    return $children;
}

Given the Id of a colonel and a secretary for that colonel, it deletes the secretary, then it saves the colonel's children to remove the colonel and return the children. After that, you can insert your elements in the root node again if you please. If you can't have the secretary Id at the moment of deleting the colonel, you can workaround the xpath to remove the secretary as child of the colonel by removing the id specification.

The RemoveNode code is a bit long, so I'll have to give you this question on how to remove nodes using SimpleXML in PHP.

I hope I can be of some help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文