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

Ask A Question

Stats

  • Questions 543k
  • Answers 543k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't use @dealerids like that, you need to use… May 17, 2026 at 6:38 am
  • Editorial Team
    Editorial Team added an answer If you want to disable the scroll bar for a… May 17, 2026 at 6:38 am
  • Editorial Team
    Editorial Team added an answer You just forgot to init your UILabel in your very… May 17, 2026 at 6:38 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to learn python and have encountered some strange behaviour. I am experimenting
i am experiencing some weird behaviour here. i am using the following code to
I'm experiencing some weird behaviour when using a ASP.NET LinkButton with a OnClientClick-property. ASPX
I am experiencing some weird encoding behaviour in my ASP.NET MVC project. In my
I was experiencing some weird behaviour in some of my javascript code, but only
In our server/client-setup we're experiencing some weird behaviour. The client is a C/C++-application which
I am experiencing a weird behavior by Scala handling of superclass constructors. I have
I'm experiencing this weird problem which my scrollbar jumps by itself to somewhere that
I am experiencing a crash while using the Boost.Spirit and Boost.Thread libraries in my
I'm experiencing really weird behavior with the Socket.Connect method in C#. I am attempting

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.