如何使用 PHP 最好使用 SimpleXML 附加到 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7098093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:00:20  来源:igfitidea点击:

How to append to a XML file with PHP preferably with SimpleXML

phpxmlinsertsimplexml

提问by salathe

I have a XML file which looks like this:

我有一个如下所示的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <config>
    </config>

    <galleries>
         // We have loads of these <gallery>
         <gallery>
             <name>Name_Here</name>
             <filepath>filepath/file.txt</filepath>
             <thumb>filepath/thumb.png</thumb>
         </gallery>
    </galleries>
</data>

I have been trying to figure out how to append another < gallery > to my above xml file. I tried using simplexml but couldn't get it to work, so I tried this answeras well as a bunchof others on stackoverflow. But just cant get it to work.
I can read from a xml file easily and get all the info I need, But I need to be able to append a gallery tag to it, The code below doesnt work and when it does, I can only insert 1 element, and it inserts it 3 times, i dont understand this.

我一直在试图弄清楚如何将另一个 <gallery> 附加到我上面的 xml 文件中。我尝试使用 simplexml 但无法让它工作,所以我在 stackoverflow 上尝试了这个答案以及一堆其他答案。但就是无法让它工作。
我可以轻松地从 xml 文件中读取并获取我需要的所有信息,但我需要能够在其中附加一个画廊标签,下面的代码不起作用,当它起作用时,我只能插入 1 个元素,然后插入它 3 次,我不明白这一点。

 $data = 'xml/config.xml';
 // Load document
 $xml = new DOMDocument;
 $xml->load( $data ); #load data into the element

 $xpath = new DOMXPath($xml);
 $results = $xpath->query('/data/galleries');
 $gallery_node = $results->item(0);

 $name_node = $xml->createElement('name');
 $name_text = $xml->createTextNode('nametext');

 $name_node = $name_node->appendChild($name_text);

 $gallery_node->appendChild($name_node);

 echo $xml->save($data);

I've had loads of failed attempts at this, this should be so easy. Basically I want to add a gallery with childs name filepath and thumb to this same file (xml/config.php).

我在这方面有很多失败的尝试,这应该很容易。基本上我想在同一个文件 (xml/config.php) 中添加一个带有孩子名称文件路径和拇指的画廊。

Like I said, I kinda got it to work, but its unformatted and a doesnt have the gallery tag.

就像我说的,我有点让它工作,但它没有格式化并且没有画廊标签。

Question
How do I insert another < gallery > (with children) into the above XML file?
Preferably even using simpleXML

问题
如何在上面的 XML 文件中插入另一个<画廊>(带孩子)?
最好甚至使用 simpleXML

回答by salathe

With SimpleXML, you can use the addChild()method.

通过 SimpleXML,您可以使用该addChild()方法。

$file = 'xml/config.xml';

$xml = simplexml_load_file($file);

$galleries = $xml->galleries;

$gallery = $galleries->addChild('gallery');
$gallery->addChild('name', 'a gallery');
$gallery->addChild('filepath', 'path/to/gallery');
$gallery->addChild('thumb', 'mythumb.jpg');

$xml->asXML($file);


Be aware that SimpleXML will not "format" the XML for you, however going from an unformatted SimpleXML representation to neatly indented XML is not a complicated step and is covered in lotsof questions here.

请注意,SimpleXML 不会为您“格式化”XML,但是从未格式化的 SimpleXML 表示到整齐缩进的 XML 并不是一个复杂的步骤,这里有很多问题。