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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:11:00+00:00 2026-05-16T23:11:00+00:00

<?xml version=1.0 encoding=utf-8?><items> <item><title>title3</title><desc>This is some desc3</desc></item></items> There is no line break between each

  • 0
<?xml version="1.0" encoding="utf-8"?><items>
<item><title>title3</title><desc>This is some desc3</desc></item></items>

There is no line break between each node element when using asXML() to output?

How to make output the file well-structured by adding a line break after each XML elements opening and closing tag that contains child element nodes:

<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<title>title3</title>
<desc>This is some desc3</desc>
</item>
</items>
  • 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-16T23:11:01+00:00Added an answer on May 16, 2026 at 11:11 pm

    The SimpleXML extension is limited to format the output, it’s sister extension, DOMDocument has support for output formatting. The XML string from your example and making use of DOMDocument::$preserveWhiteSpace and DOMDocument::$formatOutput to control the formatttings:

    $doc = new DOMDocument();
    
    $doc->preserveWhiteSpace = false;
    $doc->formatOutput = true;
    
    $doc->loadXML($string);
    
    echo $doc->saveXML();
    

    This will output a nicely indented XML with the linebreaks where you have asked for them:

    <?xml version="1.0" encoding="utf-8"?>
    <items>
      <item>
        <title>title3</title>
        <desc>This is some desc3</desc>
      </item>
      <empty/>
    </items>
    

    If you further need to manipulate the indent, you can make use of regular expressions which has been outlined in a related question and answer: Converting indentation with preg_replace (no callback).

    If you don’t want to use that method you could also switch from SimpleXML to something else and then to XMLWriter which provides a method to set the indentation (XMLWriter::setIndent) of printed XML. You would need to find an interim representation of your XML model to write it with XMLWriter however which does not look that trivial.

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

Sidebar

Related Questions

<?xml version=1.0 encoding=utf-8?> <items> <item> <title>This is title1</title> <desc>This is desc1</desc> <image></image> <tudou></tudou> </item>
Here is my XML <?xml version=1.0 encoding=UTF-8?> <menu xmlns:android=http://schemas.android.com/apk/res/android> <item android:id=@+id/refresh android:title=Home android:icon=@drawable/menu_home />
This is my XML: <?xml version=1.0 encoding=UTF-8?> <?xml-stylesheet href=test_2.xsl type=text/xsl?> <doc xmlns=http://www.foo.org> <div> <title>Mr.
I have a XML Document: <?xml version=1.0 encoding=utf-8?> <feed xmlns=http://www.w3.org/2005/Atom> <title>blahblah</title> <subtitle>blahblah.</subtitle> <link href=blahblah/>
I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
I am trying to parse the following XML with javascript: <?xml version='1.0' encoding='UTF-8'?> <ResultSet>
I created an App.config file in my WPF application: <?xml version="1.0" encoding="utf-8" ?> <configuration>
<?xml version=1.0 encoding=UTF-8?> <searchResult> <pagination> <itemsPerPage>{Number of Inventories per Page}</itemsPerPage> <numberOfItems>{Number of Inventories}</numberOfItems> </pagination>
I am trying to de-serialize the following XML file. <?xml version=1.0 encoding=UTF-8?> <searchResult> <pagination>
I have XML shaped like the following: <?xml version=1.0 encoding=UTF-8?> <feed xmlns=http://www.w3.org/2005/Atom xmlns:openSearch=http://a9.com/-/spec/opensearch/1.1/ xmlns:docs=http://schemas.google.com/docs/2007

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.