Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 888153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:20:56+00:00 2026-05-15T13:20:56+00:00

My name Rithy. I don’t know how to add new line before appending a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:20:57+00:00Added an answer on May 15, 2026 at 1:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Name: <%= Html.TextBox(txtName, 20, new { @class = hello }) %> I want that
[ { name: John Doe, location: { name: New York, New York, id: 12746342329
The name pretty much says it all. Does anyone know how to accomplish functional
[{name:Mark,Surname:Gaux}] [{Job:2,Type:Office}] I have this JSON echoed by a PHP file on the server
What name convention the new images have to have to be loaded by the
name says it all, but I want to be able to position an element
My name is Alex and I'm new to VB. I'm sorry but I am
id name mydate 1 co 2011-02-10 07:25:02 2 Carl 2011-02-10 07:26:02 . . .
The name says it all really. I suspect that insertion sort is best, since
string name = bob; object obj = name; obj = joe; Console.WriteLine(name); I'm a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.