Reputation: 159
I currently am using simplexml_load_file(file.xml)
to load an XML and $xmlRoot->saveXML('file.xml')
to save an XML File,
The code does my goals but when ever I open the XML File where it saved, it always has either spaces from deleting nodes and/or very long lines of elements from adding nodes (1 parent element and all child elements in 1 line).
My question is how do I format it in a clean format via php code?
(I want something similar to C#'s XMLDocument
class where it formats itself when saved)
My current XML File is similar to this:
<rootElement>
<parentElement><childElement>data</childElement></parentElement>
<parentElement><childElement>data</childElement></parentElement>
</rootElement>
I want it to become something like this:
<rootElement>
<parentElement>
<childElement>data</childElement>
</parentElement>
<parentElement>
<childElement>data</childElement>
</parentElement>
</rootElement>
It looks clean now, but i have quite a large XML File which has multiple child elements per parent element
Thanks and cheers
Upvotes: 0
Views: 41
Reputation: 959
Try to import the simpleXml into a DOMDocument, which offers some formatting options:
$simpleXml = simplexml_load_file('file.xml');
$xmlDom = new DOMDocument('1.0');
$xmlDom->formatOutput = true;
$xmlDom->preserveWhiteSpace = false;
$xmlDom->loadXML($simpleXml->asXML());
echo $xmlDom->saveXML();
Regards,
Upvotes: 1