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

发布于 2024-11-30 08:52:46 字数 1601 浏览 0 评论 0原文

我有一个如下所示的 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>

我一直在试图弄清楚如何附加另一个 <<画廊>到我上面的 xml 文件。我尝试使用 simplexml 但无法让它工作,所以我也尝试了这个 answer作为 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);

我已经尝试过很多次失败了,这应该很容易。基本上我想添加一个带有孩子名字文件路径和拇指的画廊到同一个文件(xml/config.php)。

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

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

I have a XML file which looks like this:

<?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 answer as well as a bunch of 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.

 $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).

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

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

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

发布评论

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

评论(1

捎一片雪花 2024-12-07 08:52:46

对于 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);

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

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

$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 lots of questions here.

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