search
Homephp教程php手册php读取xml文件的三种实现方法

文章介绍了三种方式来读取xml文件分别是new DOMDocument(),正则解析xml,用parser函数来读取xml数据,这些方法都是可行的,但第一种和最后一种要好一些.

new DOMDocument()实例代码如下:

<?php
$doc = new DOMDocument();
$doc->load(&#39;books.xml&#39;);
$books = $doc->getElementsByTagName("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";
}
?>

正则解析,代码如下:

<?php
$xml = "";
$f = fopen(&#39;books.xml&#39;, &#39;r&#39;);
while ($data = fread($f, 4096)) {
    $xml.= $data;
}
fclose($f);
preg_match_all("//<book/>(.*?)/<//book/>/s", $xml, $bookblocks);
foreach ($bookblocks[1] as $block) {
    preg_match_all("//<author/>(.*?)/<//author/>/", $block, $author);
    preg_match_all("//<title/>(.*?)/<//title/>/", $block, $title);
    preg_match_all("//<publisher/>(.*?)/<//publisher/>/", $block, $publisher);
    echo ($title[1][0] . " - " . $author[1][0] . " - " . $publisher[1][0] . "/n");
}
?>

books.xml文件如下:

<books> 
  <book> 
  <author>Jack Herrington</author> 
  <title>PHP Hacks</title> 
  <publisher>O&#39;Reilly</publisher> 
  </book> 
  <book> 
  <author>Jack Herrington</author> 
  <title>Podcasting Hacks</title> 
  <publisher>O&#39;Reilly</publisher> 
  </book> 
</books>

下面就给大家举一个小小的例子用parser函数来读取xml数据,代码如下:

<?php
$parser = xml_parser_create(); //创建一个parser编辑器
xml_set_element_handler($parser, "startElement", "endElement"); //设立标签触发时的相应函数 这里分别为startElement和endElenment
xml_set_character_data_handler($parser, "characterData"); //设立数据读取时的相应函数
$xml_file = "1.xml"; //指定所要读取的xml文件,可以是url
$filehandler = fopen($xml_file, "r"); //打开文件
while ($data = fread($filehandler, 4096)) {
    xml_parse($parser, $data, feof($filehandler));
} //每次取出4096个字节进行处理
fclose($filehandler);
xml_parser_free($parser); //关闭和释放parser解析器
$name = false;
$position = false;
function startElement($parser_instance, $element_name, $attrs) //起始标签事件的函数
{
    global $name, $position;
    if ($element_name == "NAME") {
        $name = true;
        $position = false;
        echo "名字:";
    }
    if ($element_name == "POSITION") {
        $name = false;
        $position = true;
        echo "职位:";
    }
}
function characterData($parser_instance, $xml_data) //读取数据时的函数
{
    global $name, $position;
    if ($position) echo $xml_data . "<br>";
    if ($name) echo $xml_data . "<br>";
}
function endElement($parser_instance, $element_name) //结束标签事件的函数
{
    global $name, $position;
    $name = false;
    $position = false;
}
?>

xml文件代码如下:

<?xml version="1.0" 
<employees> 
<employee> 
<name>张三</name> 
<position age="45">经理</position> 
</employee> 
<employees> 
<employee> 
<name>李四</name> 
<position age="45">助理</position> 
</employee> 
</employees>

parser是php内置的一个用来处理xml的解析器,它的工作由三个事件组成:起始标签、 读取数据、结束标签.

也就是说在对xml进行处理的时候每当遇到起始标签、数据和结束标签的时候函数会做相应的动作来完成对xml数据的转换.


教程链接:

随意转载~但请保留教程地址★

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software