PHP读取XML格式文件的方法总结
(编辑:jimmy 日期: 2025/10/26 浏览:3 次 )
本文实例总结了PHP读取XML格式文件的方法。分享给大家供大家参考,具体如下:
books.xml文件:
<books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publisher>O'Reilly</publisher> </book> </books>
1.DOMDocument方法
<"book" );
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "author" );
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publisher\n";
echo "<br>";
}
"htmlcode">
<"startElement", "endElement" );
xml_set_character_data_handler( $parser, "textData" );
$f = fopen( 'books.xml', 'r' );
while( $data = fread( $f, 4096 ) )
{
xml_parse( $parser, $data );
}
xml_parser_free( $parser );
foreach( $g_books as $book )
{
echo $book['TITLE']." - ".$book['AUTHOR']." - ";
echo $book['PUBLISHER']."\n";
}
"htmlcode">
<"";
$f = fopen( 'books.xml', 'r' );
while( $data = fread( $f, 4096 ) ) {
$xml .= $data;
}
fclose( $f );
preg_match_all( "/\<book\>(.*", $xml, $bookblocks );
foreach( $bookblocks[1] as $block )
{
preg_match_all( "/\<author\>(.*", $block, $author );
preg_match_all( "/\<title\>(.*", $block, $title );
preg_match_all( "/\<publisher\>(.*", $block, $publisher );
echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."\n" );
}
"htmlcode">
<"<root><line /><content language=\"gb2312\">简单的XML数据</content></root>";
$parser = xml_parser_create(); //创建解析器
xml_parse_into_struct($parser, $data, $values, $index); //解析到数组
xml_parser_free($parser); //释放资源
//显示数组结构
echo "\n索引数组\n";
print_r($index);
echo "\n数据数组\n";
print_r($values);
"htmlcode">
<"bb.xml";
if (!($fp = fopen($xmlfile, "r")))
{
die("无法读取XML文件$xmlfile");
}
//解析XML文件
$has_error = false; //标志位
while ($data = fread($fp, 4096))
{
//循环地读入XML文档,只到文档的EOF,同时停止解析
if (!xml_parse($xml_parser, $data, feof($fp)))
{
$has_error = true;
break;
}
}
if($has_error)
{
echo "该XML文档是错误的!<br />";
//输出错误行,列及其错误信息
$error_line = xml_get_current_line_number($xml_parser);
$error_row = xml_get_current_column_number($xml_parser);
$error_string = xml_error_string(xml_get_error_code($xml_parser));
$message = sprintf("[第%d行,%d列]:%s",
$error_line,
$error_row,
$error_string);
echo $message;
}
else
{
echo "该XML文档是结构良好的。";
}
//关闭XML解析器指针,释放资源
xml_parser_free($xml_parser);
"htmlcode">
<"1.0" encoding="UTF-8" "htmlcode">
<"test.xml");
if(strpos($file, '<"/(561)\d{1,2}/", $MESSAGE, $code);
switch($code[0]) {
case 5618 :
$myData[message] = $MESSAGE;
break;
default :
$myData[] = '没有短消息。';
break;
}
} else {
echo "加载xml文件错误。";
}
} catch(exception $e){
print_r($e);
}
} else {
echo "没有该XML文件。";
}
echo "<pre>";
print_r($myData);
echo "<hr>";
echo $myData[message];
"color: #800000">PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformat
XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
上一篇:php自定义截取中文字符串-utf8版
下一篇:PHP批量修改文件名称的方法分析
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。