I’m creating a XML file to be sent via HTTP to an application which is advising that the following code is producing malformed XML.
Can anybody advise where I’m going wrong?
$doc = new DOMDocument();
// Creating
$admin = $doc->createElement('AdminRequest');
// Add to doc
$doc->appendChild($admin);
// Creating
$secretAttr = $doc->createAttribute('secret');
$secretAttr->value = $this->secret;
// Append to Admin
$admin->appendChild($secretAttr);
// Creating
$versionAttr = $doc->createAttribute('version');
$versionAttr->value = '3.6';
// Append to admin
$admin->appendChild($versionAttr);
// Creating
$del = $doc->createElement('Delete');
// Appending to Admin
$admin->appendChild($del);
// Creating
$user = $doc->createElement('User');
$nameAttr = $doc->createAttribute('name');
$nameAttr->value = $username;
// Appending to User
$user->appendChild($nameAttr);
// Appending to delete
$del->appendChild($user);
// For debugging
echo $doc->saveXML();
The output is as follows:
<?xml version="1.0"?>
<AdminRequest secret="secretcode" version="3.6"><Delete><User name="auser"/></Delete></AdminRequest>
I have a feeling it’s something to do with the AdminRequest being on a seperate line to the first just not sure how to get it up a line.
Any help appreciated.
Thanks
Firstly, the XML is fine. Not sure how it could be interpreted as “malformed”. Newlines shouldn’t affect anything.
Secondly, are you sure that this “malformed” message of theirs isn’t just a case of your XML not conforming to a pre-defined schema?
Thirdly, what content-type are you sending? application/xml or text/xml? Because I’ve run into issues where certain applications only accepted one or the other.
Fourthly, are you sure that you’re actually sending this XML correctly? Have you tried sending it to a URL on your development so that you can parse it and validate it yourself?