这两天再看《php5权威编程》看到第8章第8.3节,书中的代码:<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>XML Example</title>
</head>
<body background="bg.png">
<p>
Moved to <a href="http://example.org/">example.org</a>.
<br />
foo & bar
</p>
</body>
</html>
保存成book_test1.xhtml<?php
/*
* Created on 2012-9-13
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
$xml = xml_parser_create('UTF-8');xml_set_element_handler($xml, 'start_handler', 'end_handler');
xml_set_character_data_handler($xml, 'character_handler');
function start_handler ($xml, $tag, $attributes)
{
global $level; echo "\n". str_repeat(' ', $level). ">>>$tag";
foreach ($attributes as $key => $value) {
echo " $key $value";
}
$level++;
}
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);
function end_handler ($xml, $tag)
{
global $level; $level--;
echo @str_repeat(' ', $level, ' '). "<<<$tag";
}function character_handler ($xml, $data)
{
global $level; $data = @split("\n", wordwrap($data, 76 - ($level * 2)));
foreach ($data as $line) {
echo @str_repeat(($level + 1), ' '). $line. "\n";
}
}
xml_parse($xml, file_get_contents('book_test1.xhtml'));
?>
保存成book_sax_xml.php
然后运行结果:
>>>html xmlns http://www.w3.org/1999/xhtml xml:lang en lang en
>>>head
>>>titleXML Example
<<<title
<<<head
>>>body background bg.png
>>>p
Moved to >>>a href http://example.org/example.org
<<<a. >>>br<<<br
foo
&
bar
<<<p
<<<body<<<html
并不是书上的:
>>>HTML XMLNS='http://www.w3.org/1999/xhtml' XML:LANG='en' LANG='en'
||
|| | | >>>HEAD
||
|| | | >>>TITLE
|XML Example| <<<TITLE...
其中有几处错误我都加了@忽略了:比如split函数str_repeat函数。这是我自己修改的book_sax_xml.php<?php
/*
* Created on 2012-9-7
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/ $xml = xml_parser_create('utf-8');
xml_set_element_handler($xml, 'start_handler', 'end_handler');
xml_set_character_data_handler($xml, 'character_handler');
function start_handler ($xml, $tag, $attributes)
{
global $level;
// $level=0;
echo "\n" . str_repeat(' ', $level) . ">>>$tag";
foreach ($attributes as $key => $value) {
echo "$key $value";
}
$level++;
}
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING,false);
function end_handler ($xml, $tag)
{
global $level;
$level--;
echo str_repeat(' ', $level)."<<<$tag"; //改正了str_repeat函数的用法,原来的错误
}
function character_handler ($xml, $data)
{
global $level;
$data = explode ("\n", wordwrap($data,76 - ($level * 2))); // 替换了函数split己经不用了
foreach ($data as $line){
echo str_repeat (' ',($level + 1)) .$line."\n"; //改正了str_repeat函数的用法,原来的错误
}
}
xml_parse($xml , file_get_contents('book_test1.xhtml'));
?>运行如下:
>>>htmlxmlns http://www.w3.org/1999/xhtmlxml:lang enlang en
>>>head
>>>title XML Example // 这里怎么没有另写一行?
<<<title
<<<head
>>>bodybackground bg.png
>>>p
Moved to >>>ahref http://example.org/ example.org // 这里怎么a 和 href粘在一起了
<<<a .
>>>br <<<br
foo
&
bar
<<<p
<<<body
<<<html
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>XML Example</title>
</head>
<body background="bg.png">
<p>
Moved to <a href="http://example.org/">example.org</a>.
<br />
foo & bar
</p>
</body>
</html>
保存成book_test1.xhtml<?php
/*
* Created on 2012-9-13
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
$xml = xml_parser_create('UTF-8');xml_set_element_handler($xml, 'start_handler', 'end_handler');
xml_set_character_data_handler($xml, 'character_handler');
function start_handler ($xml, $tag, $attributes)
{
global $level; echo "\n". str_repeat(' ', $level). ">>>$tag";
foreach ($attributes as $key => $value) {
echo " $key $value";
}
$level++;
}
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);
function end_handler ($xml, $tag)
{
global $level; $level--;
echo @str_repeat(' ', $level, ' '). "<<<$tag";
}function character_handler ($xml, $data)
{
global $level; $data = @split("\n", wordwrap($data, 76 - ($level * 2)));
foreach ($data as $line) {
echo @str_repeat(($level + 1), ' '). $line. "\n";
}
}
xml_parse($xml, file_get_contents('book_test1.xhtml'));
?>
保存成book_sax_xml.php
然后运行结果:
>>>html xmlns http://www.w3.org/1999/xhtml xml:lang en lang en
>>>head
>>>titleXML Example
<<<title
<<<head
>>>body background bg.png
>>>p
Moved to >>>a href http://example.org/example.org
<<<a. >>>br<<<br
foo
&
bar
<<<p
<<<body<<<html
并不是书上的:
>>>HTML XMLNS='http://www.w3.org/1999/xhtml' XML:LANG='en' LANG='en'
||
|| | | >>>HEAD
||
|| | | >>>TITLE
|XML Example| <<<TITLE...
其中有几处错误我都加了@忽略了:比如split函数str_repeat函数。这是我自己修改的book_sax_xml.php<?php
/*
* Created on 2012-9-7
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/ $xml = xml_parser_create('utf-8');
xml_set_element_handler($xml, 'start_handler', 'end_handler');
xml_set_character_data_handler($xml, 'character_handler');
function start_handler ($xml, $tag, $attributes)
{
global $level;
// $level=0;
echo "\n" . str_repeat(' ', $level) . ">>>$tag";
foreach ($attributes as $key => $value) {
echo "$key $value";
}
$level++;
}
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING,false);
function end_handler ($xml, $tag)
{
global $level;
$level--;
echo str_repeat(' ', $level)."<<<$tag"; //改正了str_repeat函数的用法,原来的错误
}
function character_handler ($xml, $data)
{
global $level;
$data = explode ("\n", wordwrap($data,76 - ($level * 2))); // 替换了函数split己经不用了
foreach ($data as $line){
echo str_repeat (' ',($level + 1)) .$line."\n"; //改正了str_repeat函数的用法,原来的错误
}
}
xml_parse($xml , file_get_contents('book_test1.xhtml'));
?>运行如下:
>>>htmlxmlns http://www.w3.org/1999/xhtmlxml:lang enlang en
>>>head
>>>title XML Example // 这里怎么没有另写一行?
<<<title
<<<head
>>>bodybackground bg.png
>>>p
Moved to >>>ahref http://example.org/ example.org // 这里怎么a 和 href粘在一起了
<<<a .
>>>br <<<br
foo
&
bar
<<<p
<<<body
<<<html
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货