How to output the xml generated by dom in php to the page $ Id = $ _ GET ['id'];
$ N = $ _ GET ['n'];
// Contains the database connection file
Include ('Conn. php ');
$ Dom = new DomDocument ('1. 0', 'utf-8 ');
$ Dom-> formatOutput = true;
If ($ category = ""){
Exit;
};
// Create the root node
$ Element = $ dom-> createElement ('element ');
$ Dom-> appendchild ($ element );
$ Element-> setAttribute ('N', $ n );
$ Query = mysql_query ("select * from pictures order by LevelID = '$ ID '");
While ($ result = mysql_fetch_array ($ query ))
{
$ T = $ dom-> createElement ('e ');
$ T-> setAttribute ('cr ', $ result ['c']);
$ T-> setAttribute ('isnew', $ result ['isnew']);
$ T-> setAttribute ('fileurl', $ result ['fileurl']);
$ Element-> appendChild ($ t );
Print_r ($ dom-> saveXML ());
}
Echo $ dom-> saveXML ();
?>
Neither print nor echo can output dom to the page,
How can the xml generated by dom in php be output to the page? thank you.
Reply to discussion (solution)
This is the created xml. you need some programs to read the xml.
1) DOMDocument reads xml
$ Doc = new DOMDocument ();
$ Doc-> load ('person. XML'); // read the xml file
$ Humans = $ doc-> getElementsByTagName ("humans"); // get the object array of the humans tag
Foreach ($ humans as $ human)
{
$ Names = $ human-> getElementsByTagName ("name"); // an array of objects that obtain the name tag
$ Name = $ names-> item (0)-> nodeValue; // obtain the value in node, as shown in figure
$ Sexs = $ human-> getElementsByTagName ("sex ");
$ Sex = $ sexs-> item (0)-> nodeValue;
$ Olds = $ human-> getElementsByTagName ("old ");
$ Old = $ olds-> item (0)-> nodeValue;
Echo "$ name-$ sex-$ old \ n ";
}
?>
2) simplexml reading xml
$ Xml_array = simplexml_load_file ('person. XML'); // read the data in xml to the array object.
Foreach ($ xml_array as $ tmp ){
Echo $ tmp-> name. "-". $ tmp-> sex. "-". $ tmp-> old ."
";
}
?>
Send the xml doctype header
I am not reading an xml file. I read data from the database, generate an xml file, and display it on the webpage. in the loop, if I write $ dom-> save ('New _ materials. xml '); can generate xml normally, but I want to display it on a webpage, and neither echo nor print can be implemented.
Echo and print can be used, but make sure there are no other outputs.
Otherwise, the browser fails to parse because of additional content.
It is best to first release a type declaration:
Header ("Content-type: text/xml ");