Home > Backend Development > PHP Tutorial > The solution to the problem that header is required when php outputs xml, phpxmlheader_PHP tutorial

The solution to the problem that header is required when php outputs xml, phpxmlheader_PHP tutorial

WBOY
Release: 2016-07-13 10:16:59
Original
965 people have browsed it

The solution to the problem that php output xml requires header, phpxmlheader

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:

Copy code The code is as follows:
echo '

http://www.bkjia.com/
2013-01-22T11:00:18+08:00

';
exit;

Just such a piece of code, the xml output by life and death cannot pass xml verification.
Prompt error Error: Object # has no method ‘load’
Finally, after checking for a long time, it was the same as the file placed directly in 1.xml
The specific code is as follows
Copy code The code is as follows:


http://www.bkjia.com/
2013-01-22T11:00:18+08:00

Just add a header. After struggling for several hours. . . . .
The specific code is as follows
Copy code The code is as follows:
header("Content-type:text/xml");
echo '

http://www.bkjia.com/
2013-01-22T11:00:18+08:00

';
exit;

I hope this article will be helpful to everyone’s PHP programming design.

php outputs xml type as text/xml. How to enter xml mode

/*
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> >

The php file outputs xml, and in IE only the value is displayed without the element name

Normally, new versions of browsers should have this automatic parsing function. This is a w3c standard.
Just check the source file yourself.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895112.htmlTechArticleThe solution to the problem that the header is required when php outputs xml. phpxmlheader This article describes the solution to the problem that the header is required when php outputs xml. Share it with everyone for your reference. The specific method is as follows: Problem description...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template