I'm new to Restful with PHP. I have a data stored in an array as name,id . I need to produce this data to the user as xml. I'm getting my data back however is not in proper xml format.
Here is the array:
$this->books = array("Math"=>174,
"Music"=>874,
"Computing"=>348);
Here is the method that return the results:
// /Books
protected function Books($args){
header("content-type: text/xml");
if( count($args) == 0 && $this->method == "GET"){
$dom = new DomDocument();
$root = $dom->createElement("Books");
$dom->appendChild($root);
foreach($this->books as $key => $val){
$b = $dom->createElement("book");
$root->appendChild($b);
$bName = $dom->createElement("name", $key);
$b->appendChild($bName);
$bID = $dom->createElement("ID", $val);
$b->appendChild($bID);
}
$myXML= $dom->saveXML();
return $myXML;
}
The result is shown as XML, however is not well formatted:
<?xml version=\"1.0\"?>
<Books>
<book>
<name>Math<\/name>
<ID>174<\/ID><\/book>
<book>
<name>Music<\/name>
<ID>874<\/ID><\/book>
<book>
<name>Computing<\/name>
<ID>348<\/ID><\/book><\/Books>
"
Can anyone help me how to produce it in well-formed XML? why I'm getting \ before each / ? Why there is a ?