php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77890 DOMDocument XML formatting (formatOutput) doesn't seem to work
Submitted: 2019-04-14 09:52 UTC Modified: 2019-04-15 19:33 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: phpbugreport at mailinator dot com Assigned:
Status: Re-Opened Package: DOM XML related
PHP Version: 7.3.4 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpbugreport at mailinator dot com
New email:
PHP Version: OS:

 

 [2019-04-14 09:52 UTC] phpbugreport at mailinator dot com
Description:
------------
Hi,
the formatOutput property of DOMDocument class doesn't seem to work.
Tested PHP builds:
PHP 7.3.2 (cli) (built: Feb  5 2019 23:14:21) ( NTS MSVC15 (Visual C++ 2017) x86)
PHP 7.3.4 (cli) (built: Apr  2 2019 22:13:15) ( NTS MSVC15 (Visual C++ 2017) x86)


Test script:
---------------
File to process (in.xml):
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<a>
<b>
<c/>
</b>
</a>
</root>

<?php 
$dom = new DOMDocument;
$dom->load('in.xml');

$dom->formatOutput = true;
$dom->save("out.xml");
exit();
?>

Expected result:
----------------
I expect something like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
    <a>
        <b>
            <c />
        </b>
    </a>
</root>

Actual result:
--------------
The output is the same as the input
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<a>
<b>
<c/>
</b>
</a>
</root>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-15 08:17 UTC] luxian dot m at gmail dot com
In the comments section of DOMDocument there are people saying that you need to set 'preserveWhitespaces' to 'false' and 'formatOutput' before you load the file

This works:

<?php
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load('in.xml');
?>
 [2019-04-15 19:31 UTC] phpbugreport at mailinator dot com
-Status: Open +Status: Closed
 [2019-04-15 19:31 UTC] phpbugreport at mailinator dot com
Thank you, luxian. This should be added to the documentation.
 [2019-04-15 19:33 UTC] requinix@php.net
-Status: Closed +Status: Re-Opened -Type: Bug +Type: Documentation Problem -Package: XML Writer +Package: DOM XML related
 [2019-04-15 19:33 UTC] requinix@php.net
Then don't close the report...
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 00:01:26 2019 UTC