<i id='mnlBS'><tr id='mnlBS'><dt id='mnlBS'><q id='mnlBS'><span id='mnlBS'><b id='mnlBS'><form id='mnlBS'><ins id='mnlBS'></ins><ul id='mnlBS'></ul><sub id='mnlBS'></sub></form><legend id='mnlBS'></legend><bdo id='mnlBS'><pre id='mnlBS'><center id='mnlBS'></center></pre></bdo></b><th id='mnlBS'></th></span></q></dt></tr></i><div id='mnlBS'><tfoot id='mnlBS'></tfoot><dl id='mnlBS'><fieldset id='mnlBS'></fieldset></dl></div>

    1. <tfoot id='mnlBS'></tfoot>
      • <bdo id='mnlBS'></bdo><ul id='mnlBS'></ul>
    2. <small id='mnlBS'></small><noframes id='mnlBS'>

    3. <legend id='mnlBS'><style id='mnlBS'><dir id='mnlBS'><q id='mnlBS'></q></dir></style></legend>

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

      时间:2023-10-30

          <i id='EO6aa'><tr id='EO6aa'><dt id='EO6aa'><q id='EO6aa'><span id='EO6aa'><b id='EO6aa'><form id='EO6aa'><ins id='EO6aa'></ins><ul id='EO6aa'></ul><sub id='EO6aa'></sub></form><legend id='EO6aa'></legend><bdo id='EO6aa'><pre id='EO6aa'><center id='EO6aa'></center></pre></bdo></b><th id='EO6aa'></th></span></q></dt></tr></i><div id='EO6aa'><tfoot id='EO6aa'></tfoot><dl id='EO6aa'><fieldset id='EO6aa'></fieldset></dl></div>

          <small id='EO6aa'></small><noframes id='EO6aa'>

            <bdo id='EO6aa'></bdo><ul id='EO6aa'></ul>

          • <legend id='EO6aa'><style id='EO6aa'><dir id='EO6aa'><q id='EO6aa'></q></dir></style></legend>
                <tbody id='EO6aa'></tbody>
              <tfoot id='EO6aa'></tfoot>
                本文介绍了如何使用 PHP 附加到 XML 文件,最好使用 SimpleXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                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>
                

                我一直在试图弄清楚如何附加另一个 <画廊 > 到我上面的 xml 文件.我尝试使用 simplexml 但无法让它工作,所以我也尝试了这个 answer作为stackoverflow上的一堆.但就是不能让它工作.
                我可以轻松地从 xml 文件中读取并获取我需要的所有信息,但我需要能够向其附加画廊标签,下面的代码不起作用,当它起作用时,我只能插入 1 个元素,它会插入看了3遍,没看懂.

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

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

                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.

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

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

                推荐答案

                有了 SimpleXML,你可以使用 addChild() 方法.

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

                <小时>

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

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

                上一篇:PHP mySQL - 将新记录插入表中,主键自动递增 下一篇:使用日期循环 SQL 插入语句

                相关文章

                  <bdo id='9Ku8a'></bdo><ul id='9Ku8a'></ul>

                  1. <small id='9Ku8a'></small><noframes id='9Ku8a'>

                    <legend id='9Ku8a'><style id='9Ku8a'><dir id='9Ku8a'><q id='9Ku8a'></q></dir></style></legend>
                  2. <i id='9Ku8a'><tr id='9Ku8a'><dt id='9Ku8a'><q id='9Ku8a'><span id='9Ku8a'><b id='9Ku8a'><form id='9Ku8a'><ins id='9Ku8a'></ins><ul id='9Ku8a'></ul><sub id='9Ku8a'></sub></form><legend id='9Ku8a'></legend><bdo id='9Ku8a'><pre id='9Ku8a'><center id='9Ku8a'></center></pre></bdo></b><th id='9Ku8a'></th></span></q></dt></tr></i><div id='9Ku8a'><tfoot id='9Ku8a'></tfoot><dl id='9Ku8a'><fieldset id='9Ku8a'></fieldset></dl></div>
                    <tfoot id='9Ku8a'></tfoot>