How to output the xml generated by dom in php to the page

Source: Internet
Author: User
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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.