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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:58:48+00:00 2026-05-27T16:58:48+00:00

Here are the codes: $doc = new DomDocument(‘1.0’); // create root node $root =

  • 0

Here are the codes:

$doc = new DomDocument('1.0');
// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
$signed_values = array('a' => 'eee', 'b' => 'sd', 'c' => 'df');
// process one row at a time
foreach ($signed_values as $key => $val) {
    // add node for each row
    $occ = $doc->createElement('error');
    $occ = $root->appendChild($occ);
    // add a child node for each field
    foreach ($signed_values as $fieldname => $fieldvalue) {
        $child = $doc->createElement($fieldname);
        $child = $occ->appendChild($child);
        $value = $doc->createTextNode($fieldvalue);
        $value = $child->appendChild($value);
    }
}
// get completed xml document
$xml_string = $doc->saveXML() ;
echo $xml_string;

If I print it in the browser I don’t get nice XML structure like

<xml> \n tab <child> etc.

I just get

<xml><child>ee</child></xml>

And I want to be utf-8
How is this all possible to do?

  • 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-27T16:58:49+00:00Added an answer on May 27, 2026 at 4:58 pm

    You can try to do this:

    ...
    // get completed xml document
    $doc->preserveWhiteSpace = false;
    $doc->formatOutput = true;
    $xml_string = $doc->saveXML();
    echo $xml_string;
    

    You can make set these parameter right after you’ve created the DOMDocument as well:

    $doc = new DomDocument('1.0');
    $doc->preserveWhiteSpace = false;
    $doc->formatOutput = true;
    

    That’s probably more concise. Output in both cases is (Demo):

    <?xml version="1.0"?>
    <root>
      <error>
        <a>eee</a>
        <b>sd</b>
        <c>df</c>
      </error>
      <error>
        <a>eee</a>
        <b>sd</b>
        <c>df</c>
      </error>
      <error>
        <a>eee</a>
        <b>sd</b>
        <c>df</c>
      </error>
    </root>
    

    I’m not aware how to change the indentation character(s) with DOMDocument. You could post-process the XML with a line-by-line regular-expression based replacing (e.g. with preg_replace):

    $xml_string = preg_replace('/(?:^|\G)  /um', "\t", $xml_string);
    

    Alternatively, there is the tidy extension with tidy_repair_string which can pretty print XML data as well. It’s possible to specify indentation levels with it, however tidy will never output tabs.

    tidy_repair_string($xml_string, ['input-xml'=> 1, 'indent' => 1, 'wrap' => 0]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's my code: XmlDocument doc = new XmlDocument(); foreach (string c in colorList) {
Here is the code currently used. public String getStringFromDoc(org.w3c.dom.Document doc) { try { DOMSource
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
Here I'm pasting my codes where I want to retrieve Bundle version from my
I'm using an HTML sanitizing whitelist code found here: http://refactormycode.com/codes/333-sanitize-html I needed to add
Yes we're talking about ASCII codes. My appologies I'm not the Delphi dev here.
I need to create an XmlDocument with a root element containing multiple namespaces. Am
The example on codeplex is this : HtmlDocument doc = new HtmlDocument(); doc.Load(file.htm); foreach(HtmlNode
i have the following code: var doc = new XmlDocument(); XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration(1.0,
Ok the error is showing up somewhere in this here code if($error==false) { $query

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.