Adding namespace to XML



I'm creating XML response for the one of our clients with the namespace URLs in that using PHP. I'm expecting the output as follows,



<?xml version="1.0" encoding="UTF-8"?>
<ns3:userResponse xmlns:ns3="http://ift.tt/ra1lAU" xmlns:ns2="http://ift.tt/tphNwY">
<Content>
<field1>fieldvalue1</field1>
</Content>
</ns3:userResponse>


But by using the following code,



<?php
// create a new XML document
$doc = new DomDocument('1.0', 'UTF-8');

// create root node
$root = $doc->createElementNS('http://ift.tt/ra1lAU', 'ns3:userResponse');
$root = $doc->appendChild($root);

$root->setAttributeNS('http://ift.tt/ra1lAU', 'ns1:schemaLocation','');
$root->setAttributeNS('http://ift.tt/tphNwY', 'ns2:schemaLocation','');

// add node for each row
$occ = $doc->createElement('Content');
$occ = $root->appendChild($occ);


$child = $doc->createElement("field1");
$child = $occ->appendChild($child);

$value = $doc->createTextNode('fieldvalue1');
$value = $child->appendChild($value);


// get completed xml document
$xml_string = $doc->saveXML();

echo $xml_string;


I'm getting the response as,



<?xml version="1.0" encoding="UTF-8"?>
<ns3:userResponse xmlns:ns3="http://ift.tt/ra1lAU" xmlns:ns2="http://ift.tt/tphNwY" ns3:schemaLocation="" ns2:schemaLocation="">
<Content>
<field1>fieldvalue1</field1>
</Content>
</ns3:userResponse>


Here the problem is, the third attribute is mandatory attribute for the function setAttributeNS PHP function. Is there anyway to remove that ns3:schemaLocation and ns2:schemaLocation? I googled a lot but couldn't able to find any useful answers. So, any idea on this would be so great?


No comments:

Post a Comment