如何使用PHP最好使用SimpleXML附加到XML文件 [英] How to append to a XML file with PHP preferably with SimpleXML

查看:113
本文介绍了如何使用PHP最好使用SimpleXML附加到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的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);

我对此尝试失败了很多,这应该很容易.基本上,我想将带有孩子名称filepath和thumb的画廊添加到同一文件(xml/config.php).

就像我说的那样,我确实可以使用它,但是它没有格式并且没有画廊标签.

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

解决方案

通过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缩进并不是一个复杂的步骤,并且在很多问题中进行了介绍. /p>

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

解决方案

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.

这篇关于如何使用PHP最好使用SimpleXML附加到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆