The example in this article describes the solution to the problem that header is required when php outputs xml. Share it with everyone for your reference. The specific method is as follows:
Problem description:
Recently, when doing an xml output, I found that the xml document we input directly using echo will prompt Error: Object # has no method 'load' error. Later, I changed to using header() to output the xml header file and the problem was solved. Record it below.
Solution:
Because the length of xml is too similar to that of txt file, I always confuse it with txt.
Now let’s take a look at the following code:
I hope this article will be helpful to everyone’s PHP programming design.
/*
Write xml file
*/
mysql_connect('localhost','root','');
mysql_query("set names utf8 ");
mysql_select_db('1012javab');
$result = mysql_query("select * from goods");
while($a = mysql_fetch_assoc($result)){
$arr[ ] = $a;
}
// Instantiate a dom object
$doc = new DOMDocument;
// Format input
$doc->formatOutput = true ;
// Create a root node
$books = $doc->createElement('books');
// Add element node
$books = $doc-> appendChild($books);
/*
$books = $doc->appendChild($books);
$book = $doc->createElement('book');
$ books->appendChild($book);
$title = $doc->createElement('ttt');
$book->appendChild($title);
*/
foreach($arr as $key=>$val){
echo 11;
$book = $doc->createElement('book');
foreach($val as $key1=> ;$val1){
###########################First group
$title = $doc- >createElement($key1);
// Create a file node
$content = $doc->createTextNode($val1);
$title->appendChild($content);
$book->appendChild($title);
}
$books->appendChild($book);
}
if($doc->save(' goods.xml')){
echo 'yes';
}else{
echo 'no';
}
/*
$doc = new DOMDocument( '1.0');
// we want a nice output
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title...the rest of the text> >
Normally, new versions of browsers should have this automatic parsing function. This is a w3c standard.
Just check the source file yourself.