公告信息1
admin
公告信息2
管理员
$doc = new DOMDocument();
$doc->load('board.xml' );
$infors = $doc->getElementsByTagName("information" );
foreach( $inforsas $infor )
{
$values = $infor->getElementsByTagName("value");
$value = $values->item(0)->nodeValue;
$authors =$infor->getElementsByTagName("author" );
$author =$authors->item(0)->nodeValue;
echo"$value - $author \n";
}?>
$parser = xml_parser_create();//创建一个parser编辑器
xml_set_element_handler($parser,"startElement","endElement");//设立标签触发时的相应函数这里分别为startElement和endElenment
xml_set_character_data_handler($parser,"characterData");//设立数据读取时的相应函数
$xml_file='board.xml';//指定所要读取的xml文件,可以是url
if(!($fp = fopen($xml_file,"r"))) {
die("could not open XMLinput");
}$filehandler = fopen($xml_file,"r");//打开文件
$name=false;
$position=false;
while ($data =fread($filehandler, 4096))
{
xml_parse($parser, $data,feof($filehandler));
if (!xml_parse($parser, $data, feof($filehandler))){
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
}//每次取出4096个字节进行处理fclose($filehandler);
xml_parser_free($parser);//关闭和释放parser解析器function startElement($parser_instance, $element_name, $attrs) //起始标签事件的函数
{
global $name,$position;
if($element_name=="author")
{
$name=true;
$position=false;
echo"名字:";
}
if($element_name=="value")
{$name=false;
$position=true;
echo"信息:";
}
}function characterData($parser_instance, $xml_data) //读取数据时的函数
{
global $name,$position;
if($position)
echo$xml_data."
";
if($name)
echo$xml_data."
";
}function endElement($parser_instance, $element_name) //结束标签事件的函数
{
global $name,$position;
$name=false;
$position=false;
}?>
结果还是只能用UTF-8编码来看结果。
The PHP function such as headers(), used with start_session(), producesHTML headers. When the PHP fragments are placed in the body of the HTML, thiscauses errors because the PHP fragments are trying to put HTML headers withinthe HTML body. Solution:Put the PHP fragments before the HTML body tag.(link:http://parallel.hpc.unsw.edu.au/~chee/IdiotErrors/node9.html)
引用了UTF-8格式的文件,有些UTF-8格式的文件会在文件的开始放入判断字元(BOM),可以启用PHP.INI的设定 output_buffering= 4096(link:http://info.mysql.cn/php/2006/0323/5499.html)