本文实例总结了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,读取,XML格式文件
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“PHP读取XML格式文件的方法总结”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2025年11月06日
2025年11月06日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]