使用 PHP simpleXml 将子项添加到 xml

发布于 2024-09-08 08:29:40 字数 933 浏览 1 评论 0原文

我对 simpleXml 和添加新项目有疑问。这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <items>               
    <item>abc</item>
    <item>def</item>
    <item>ghi</item>
</items>
</root>

我使用这个 php 代码:

$xml = simplexml_load_file("myxml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$newItem = $sxe->addChild("items");
$newItem->addChild("item", $newValue);
$sxe->asXML("myxml.xml"); 

这是结果:

<?xml version="1.0" encoding="utf-8"?>
    <root>
      <items>               
        <item>abc</item>
        <item>def</item>
        <item>ghi</item>
      </items>
      <items>
        <item>jkl</item>
      </items>
    </root>

这会创建新的项目节点,但我想将项目添加到相同的现有项目节点。

i have problem with simpleXml and adding new items. This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <items>               
    <item>abc</item>
    <item>def</item>
    <item>ghi</item>
</items>
</root>

Im using this php code:

$xml = simplexml_load_file("myxml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$newItem = $sxe->addChild("items");
$newItem->addChild("item", $newValue);
$sxe->asXML("myxml.xml"); 

This is the result:

<?xml version="1.0" encoding="utf-8"?>
    <root>
      <items>               
        <item>abc</item>
        <item>def</item>
        <item>ghi</item>
      </items>
      <items>
        <item>jkl</item>
      </items>
    </root>

This creates me new items node, but i want add item to the same already existing items node.

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

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

发布评论

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

评论(3

眼眸里的快感 2024-09-15 08:29:40

那么,您不应该创建新的项目节点:

$xml = simplexml_load_file("myxml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$itemsNode = $sxe->items[0];
$itemsNode->addChild("item", $newValue);
$sxe->asXML("myxml.xml"); 

then, you should not create new items node:

$xml = simplexml_load_file("myxml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$itemsNode = $sxe->items[0];
$itemsNode->addChild("item", $newValue);
$sxe->asXML("myxml.xml"); 
要走干脆点 2024-09-15 08:29:40

您是否尝试过执行以下方式

$newItem->root->items[0]->addChild("item","Test");

$newItem->root->items->addChild("item","Test");

Have you tried doing the following way

$newItem->root->items[0]->addChild("item","Test");

Or

$newItem->root->items->addChild("item","Test");
迷雾森÷林ヴ 2024-09-15 08:29:40

您可以将此类用于接受子追加的 SimpleXML 对象

    <?php

    class MySimpleXMLElement extends SimpleXMLElement
    {
        /**
         * Add SimpleXMLElement code into a SimpleXMLElement
         *
         * @param MySimpleXMLElement $append
         */
        public function appendXML($append)
        {
            if ($append) {
                if (strlen(trim((string)$append)) == 0) {
                    $xml = $this->addChild($append->getName());
                } else {
                    $xml = $this->addChild($append->getName(), (string)$append);
                }

                foreach ($append->children() as $child) {
                    $xml->appendXML($child);
                }

                foreach ($append->attributes() as $n => $v) {
                    $xml->addAttribute($n, $v);
                }
            }
        }
    }

You can use this class to SimpleXML objects that accept children append

    <?php

    class MySimpleXMLElement extends SimpleXMLElement
    {
        /**
         * Add SimpleXMLElement code into a SimpleXMLElement
         *
         * @param MySimpleXMLElement $append
         */
        public function appendXML($append)
        {
            if ($append) {
                if (strlen(trim((string)$append)) == 0) {
                    $xml = $this->addChild($append->getName());
                } else {
                    $xml = $this->addChild($append->getName(), (string)$append);
                }

                foreach ($append->children() as $child) {
                    $xml->appendXML($child);
                }

                foreach ($append->attributes() as $n => $v) {
                    $xml->addAttribute($n, $v);
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文