公告信息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)