Skip to content
Advertisement

PHP XML how to output nice format

Here are the codes:

JavaScript

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

JavaScript

I just get

JavaScript

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

Advertisement

Answer

You can try to do this:

JavaScript

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

JavaScript

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

JavaScript

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):

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement