在PHP中以格式化方式编辑XML

在PHP中以格式化方式编辑XML,php,xml,Php,Xml,我是PHP新手,我想用PHP编辑XML文件。当我在xml文件中添加child时,我没有以正确的格式获取它们。这会在文件中进一步编辑时产生问题日志。请帮助我编辑xml文件,以便生成的xml格式良好 提前谢谢 我正在使用以下代码编辑xml文件 <?php $xml_file = simplexml_load_file('editing.xml'); $xml_file->formatOutput = true; $xml_file->preserveWhiteSpace =

我是PHP新手,我想用PHP编辑XML文件。当我在xml文件中添加child时,我没有以正确的格式获取它们。这会在文件中进一步编辑时产生问题日志。请帮助我编辑xml文件,以便生成的xml格式良好

提前谢谢

我正在使用以下代码编辑xml文件

<?php

$xml_file = simplexml_load_file('editing.xml');

$xml_file->formatOutput = true;

$xml_file->preserveWhiteSpace = false;

$xml_file->addChild('article',' ')->addAttribute('id', 'id3');

file_put_contents('editing.xml', $xml_file->asXML());
?>

这是我正在使用的xml文件

<?xml version="1.0" encoding="utf-8"?>
<userva>
    <user id="BPL000001" first_name="f_name" last_name="l_name">

        <article id="wersomething">
            <title>Some title</title>
            <description>he does play well all the time</description>
            <posted_on>11/Nov/2012</posted_on>
            <text_contents>Some contents.. Some Contents..Some contents.. Some Contents..Some contents.. Some Contents..</text_contents>
            <images>
                <image>image1.jpg</image>
                <image>image2.jpg</image>
            </images>
            <links>
                <link>link1</link>
                <link>link2</link>
            </links>
            <likes>
                <like>like11</like>
                <like>like12</like>
                <like>like13</like>
                <like>like14</like>
                <like>like15</like>
            </likes>
            <comments>
                <comment by="523423440"/>
                <comment by="054232130"/>
                <comment by="15849584200"/>
            </comments>
       </article>

        <article id="id2">
            <title>Some title</title>
            <description>he does play well all the time</description>
            <posted_on>95956262</posted_on>
            <text_contents>Some contents.. Some Contents..Some contents.. Some Contents..Some contents.. Some Contents..</text_contents>
            <images>
                <image>imageva1.jpg</image>
                <image>imageva2.jpg</image>
            </images>
            <links>
                <link>link1</link>
                <link>link2</link>
            </links>
            <likes>
                <like>like21</like>
                <like>like22</like>
                <like>like23</like>
                <like>like24</like>
                <like>like25</like>
            </likes>
            <comments>
                <comment by="0142264340"/>
                <comment by="0523412130"/>
                <comment by="5523484200"/>
            </comments>
        </article>
    </user>
</userva>

一些头衔
他确实一直打得很好
2012年11月11日
一些内容。。一些内容一些内容。。一些内容一些内容。。一些内容。。
图1.jpg
图像2.jpg
链接1
链接2
像11
像12
像13
像14
像15
一些头衔
他确实一直打得很好
95956262
一些内容。。一些内容一些内容。。一些内容一些内容。。一些内容。。
imageva1.jpg
imageva2.jpg
链接1
链接2
像21
像22
像23
像24
像25
请参阅此

试试这个:

$dom = dom_import_simplexml($simpleXml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();
参考这个

试试这个:

$dom = dom_import_simplexml($simpleXml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();