无为清净楼资源网 Design By www.qnjia.com

本文实例总结了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格式文件

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?