There are many techniques for reading and writing XML in PHP. This article provides three ways to read XML: using a DOM library, using a SAX parser, and using regular expressions. Writing XML using DOM and PHP text templates is also covered.
Reading and writing Extensible Markup Language (XML) with PHP may seem a little scary. In fact, XML and all its related technologies can be scary, but reading and writing XML in PHP doesn't have to be a scary task. First, you need to learn a little bit about XML—what it is and what you can do with it. Then, you need to learn how to read and write XML in PHP, and there are many ways to do this.
This article provides a brief introduction to XML and then explains how to read and write XML with PHP.
What is XML?
XML is a data storage format. It does not define what data is saved, nor does it define the format of the data. XML simply defines tags and the attributes of those tags. Well-formed XML markup looks like this:
<name>Jack Herrington</name>
This
An XML tag that does not contain text looks like this:
<powerUp />
There is more than one way to write something in XML. For example, this tag forms the same output as the previous tag:
<powerUp></powerUp>
You can also add attributes to XML tags. For example, this
<name first="Jack" last="Herrington" />
You can also use XML to encode special characters. For example, the & symbol can be encoded like this:
&
An XML file containing tags and attributes, if formatted like the example, is well-formed, meaning the tags are symmetrical and the characters are encoded correctly. Listing 1 is an example of well-formed XML.
Listing 1. XML book list example
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>OReilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>OReilly</publisher>
</book>
</books>
The XML in Listing 1 contains a list of books. The parent
An XML document is correct when its markup structure and content are verified by an external schema file. Schema files can be specified in different formats. For the purposes of this article, all that is needed is well-formed XML.
If you think XML looks a lot like Hypertext Markup Language (HTML), you're right. XML and HTML are both markup-based languages and they have many similarities. However, it is important to note that while an XML document may be well-formed HTML, not all HTML documents are well-formed XML. The newline tag (br) is a good example of the difference between XML and HTML. This newline tag is well-formed HTML, but not well-formed XML:
<p>This is a paragraph<br>
With a line break</p>
This newline tag is well-formed XML and HTML:
<p>This is a paragraph<br />
With a line break</p>
If you want to write HTML as well-formed XML, follow the W3C committee's Extensible Hypertext Markup Language (XHTML) standard (see Resources). All modern browsers can render XHTML. Furthermore, you can use XML tools to read XHTML and find the data in the document, which is much easier than parsing HTML.
Use DOM library to read XML
The easiest way to read well-formed XML files is to use the Document Object Model (DOM) library compiled into some PHP installations. The DOM library reads the entire XML document into memory and represents it as a node tree, as shown in Figure 1.
Figure 1. XML DOM tree of book XML
The books node at the top of the tree has two book child tags. In each book, there are several nodes: author, publisher and title. The author, publisher, and title nodes each have text child nodes that contain text.
The code to read the book XML file and display the content using the DOM is shown in Listing 2.
List 2. Reading book XML using DOM
<?php
$doc = new DOMDocument();
$doc->load( books.xml );
$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
";
}
?>
脚本首先创建一个 new DOMdocument 对象,用 load 方法把图书 XML 装入这个对象。之后,脚本用 getElementsByName 方法得到指定名称下的所有元素的列表。
在 book 节点的循环中,脚本用 getElementsByName 方法获得 author、publisher 和 title 标记的 nodeValue。nodeValue 是节点中的文本。脚本然后显示这些值。
可以在命令行上像这样运行 PHP 脚本:
% php e1.php
PHP Hacks - Jack Herrington - OReilly
Podcasting Hacks - Jack Herrington - OReilly
%
可以看到,每个图书块输出一行。这是一个良好的开始。但是,如果不能访问 XML DOM 库该怎么办?
用 SAX 解析器读取 XML
读取 XML 的另一种方法是使用 XML Simple API(SAX)解析器。PHP 的大多数安装都包含 SAX 解析器。SAX 解析器运行在回调模型上。每次打开或关闭一个标记时,或者每次解析器看到文本时,就用节点或文本的信息回调用户定义的函数。
SAX 解析器的优点是,它是真正轻量级的。解析器不会在内存中长期保持内容,所以可以用于非常巨大的文件。缺点是编写 SAX 解析器回调是件非常麻烦的事。清单 3 显示了使用 SAX 读取图书 XML 文件并显示内容的代码。
清单 3. 用 SAX 解析器读取图书 XML
<?php
$g_books = array();
$g_elem = null;
function startElement( $parser, $name, $attrs )
{
global $g_books, $g_elem;
if ( $name == BOOK ) $g_books []= array();
$g_elem = $name;
}
function endElement( $parser, $name )
{
global $g_elem;
$g_elem = null;
}
function textData( $parser, $text )
{
global $g_books, $g_elem;
if ( $g_elem == AUTHOR ||
$g_elem == PUBLISHER ||
$g_elem == TITLE )
{
$g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;
}
}
$parser = xml_parser_create();
xml_set_element_handler( $parser, "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]."
";
}
?>
脚本首先设置 g_books 数组,它在内存中容纳所有图书和图书信息,g_elem 变量保存脚本目前正在处理的

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
