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 1021855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:22:28+00:00 2026-05-16T11:22:28+00:00

I am experiencing weird behaviour when trying to format xml output while modifying domdocument

  • 0

I am experiencing weird behaviour when trying to format xml output while modifying domdocument structure.

I have created simple Item class based on DomDocument:

class Item extends DOMDocument {

private $root;

function __construct($version = null, $encoding = null) {
    parent::__construct($version, $encoding);
    $this->formatOutput = true;
    $this->root = $this->createElement("root");
    $this->root = $this->appendChild($this->root);
}

function build($name) {
    $item = $this->createElement("item");
    $name = $this->createTextNode($name);
    $item->appendChild($name);
    $this->getElementsByTagName("root")->item(0)->appendChild($item);
}
}

Now, I have small use case here:

$it = new Item('1.0', 'iso-8859-1');
$it->build("first");
$it->build("seccond");
$xml = $it->saveXML();

echo $xml;

$it2 = new Item('1.0', 'iso-8859-1');
$it2->loadXML($xml);

$it2->build("third");
$it2->build("fourth");
$it2->build("fifth");
$it2->formatOutput = true;

$xml2 = $it2->saveXML();

echo $xml2;

And now the odd bit. I call the script and it produces two xml files as required, however I noticed that formating is somehow lost after I edit the document. It sort of carries on without any indents etc.

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
  <item>first</item>
  <item>seccond</item>
</root>
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
  <item>first</item>
  <item>seccond</item>

<item>third</item><item>fourth</item><item>fifth</item></root>

I am assuming it’s something I’m missing here. Maybe it’s the way I’m appending nodes to root after opening document, maybe some magic setting.

The code does the job, but I was wondering what would be the cause of this weird behaviour.

  • 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-16T11:22:28+00:00Added an answer on May 16, 2026 at 11:22 am

    You can “tell” libxml that leading/trailing whitespaces are not significant (and thus in this case libxml may insert whitespaces to indent elements) e.g. by setting the preserveWhiteSpace property to false.

    $this->formatOutput = true;
    $this->preserveWhiteSpace  = false;
    $this->root = $this->createElement("root");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experiencing a weird behaviour with one Button in Android. I created a Custom
I am experiencing weird behavior with template toolkit, say I have a data structure
I am experiencing a very weird problem: In WPF I have a tabControl which
I'm experiencing some really weird object behaviours at the moment. I have this template
I'm trying to learn python and have encountered some strange behaviour. I am experimenting
I am experiencing some weird encoding behaviour in my ASP.NET MVC project. In my
I am experiencing something weird and have a workaround already, but I don't think
I'm experiencing a weird issue in which I need to have a default constructor
I am experiencing some weird behavior with an android app I have been working
I have a Nivo slide gallery and am experiencing some weird inconsistency between browsers.

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.