Rithy
Rithy

Reputation: 184

PHP DOM append child with new line

My name Rithy.

I don't know how to add new line before appending a new node or element in the xml.

My php:

$dom = new DOMDocument();

$dom->formatOutput = true;

$dom->preserveWhiteSpace = true;

$dom->load($xml_file);

$body = $dom->getElementsByTagName('body')->item(0);

$newelement_seg = $dom->createElement('seg');

$data = $dom->createTextNode(" text 2 ");  

$newelement_seg->appendChild($data);

$body->appendChild($newelement_seg);

$dom->save($xml_file);

XML Before append a new child:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <seg>
        text 1
    </seg>
</body>
</xml>

XML after append a new child:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <seg>
        text 1
    </seg>
    <seg>
        text 2
    </seg>
</body>
</xml>

But I want:

<?xml version="1.0" encoding="UTF-8"?>
    <body>
        <seg>
            text 1
        </seg>
        <seg>
            text 2
        </seg>
    </body>
</xml>
<hr/>

Thanks in advance!

Upvotes: 7

Views: 13627

Answers (3)

LoopDuplicate
LoopDuplicate

Reputation: 473

TO SUMMARIZE THE ANSWERS GIVEN:

  1. you have to set formatOutput to true

  2. you have to set ignoreWhiteSpace to false

ie:

$dom = new DomDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->load($myxmlfile);

Upvotes: 12

Jayendra Gaikwad
Jayendra Gaikwad

Reputation: 41

Perfect solution. It's working. Here is the code that I have used:

       //create new document object
        $dom_object = new DOMDocument(); 

        //load xml file
        $xml_file_path = get_template_directory()."/flash/playlist.xml";
        $dom_object->formatOutput = true;    
        $dom_object->preserveWhiteSpace = false;
        $dom_object->load($xml_file_path);    

Upvotes: 3

Artefacto
Artefacto

Reputation: 97815

What are you trying to accomplish?

By setting preserveWhiteSpace to true (unnecessary; that's the default), you are telling libxml not to ignore text nodes that are composed only of white space. Yet, at the same time, you're trying to pretty format the XML file, which to be really pretty requires whitespace nodes.

Then, even ignoring white space, you have to realize the whitespace inside the <seg> tags is significant; libxml will not remove those; if you force a line break after text 1, in the next line the </seg> closing tag must not be indented, otherwise the content of the text node inside the tag would differ.

Your code gives me this:

<?xml version="1.0" encoding="UTF-8"?>
<body>
<seg>
  text 1  
</seg>
<seg> text 2 </seg></body>

Since you're telling libxml that whitespace is significant, it cannot put a line break after the second </seg>, otherwise it'd be creating another text node.

If you say $dom->preserveWhiteSpace = false;:

<?xml version="1.0" encoding="UTF-8"?>
<body>
  <seg>
  text 1  
</seg>
  <seg> text 2 </seg>
</body>

The tags are indented, but libxml still cannot do this:

<?xml version="1.0" encoding="UTF-8"?>
<body>
  <seg>
    text 1  
  </seg>
  <seg>
    text 2
  </seg>
</body>

because it would be changing the content of the text nodes inside <seg>.

You might want to try tidy, though I'm not sure it'll do what you want.

Upvotes: 2

Related Questions