user2360831
user2360831

Reputation: 367

php xml dom need whitespace before closing tag ` />`

Want to get output like this <TaksPusgads xsi:nil="true" />

Used such code

 $dom = new DOMDocument();

 $dom->appendChild( $dom->createElement("TaksPusgads") )
 ->appendChild( $dom->createAttribute("xsi:nil") )
 ->appendChild( $dom->createTextNode('true'));//

 $xml = $dom->saveXML();
 echo $xml; 

Example is here https://sandbox.onlinephpfunctions.com/code/97b175a6722bd6968c8ff715e97479925b58ea6b

But i get <TaksPusgads xsi:nil="true"/>

Instead of "true"/> need "true" /> (whitespace before />).

What code i need to add or ammend?

Upvotes: 0

Views: 139

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

If the recipient of the XML needs whitespace here, then it means it's not using a conformant XML parser.

We get lots of questions here from people trying to process XML using home-brewed parsing methods, generally relying on regular expressions, and we always tell them about the problems they are creating for people like you who are then forced to generate the peculiar subset of XML that they are able to handle.

Basically, if people only accept a subset of XML, as appears to be the case here, then you'll need to hand-craft the generation of the XML, because standard XML libraries (such as @dom->saveXML()) aren't going to do the job.

The code you need to amend is the code that processes the XML you are creating.

Upvotes: 2

Related Questions