Editing XML in formatted way in PHP

I am new to PHP and I want to edit XML files using PHP. When I add child in my xml file I don’t get them in proper formatted way. This is creating log of trouble in further editing in the file. Please help me edit my xml file so that the resulting xml is well formatted.

Thanks in advance.

I am editing my xml file using this code

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

This is the xml file I am using.

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

        <article id="wersomething">
            <title>Sachin Tendulkar - plays well</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="92296964340"/>
                <comment by="95454512130"/>
                <comment by="95849584200"/>
            </comments>
       </article>

        <article id="id2">
            <title>Sachin Tendulkar - plays well</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="92296964340"/>
                <comment by="95454512130"/>
                <comment by="95849584200"/>
            </comments>
        </article>
    </user>
</userva>

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories