Tag Archive: php

How to Parse XML File in PHP

Actually there are four relatively simple ways to read an XML file:

  • DOM which uses the DOM API (and therefore has to load the whole document into memory)
  • SimpleXML which provides a very simple and elegant way to parse XML documents (but lacks a lot of document manipulation methods), it also loads the whole document into memory
  • XMLReader is a stream-based XML pull parser. It’s usage is not as intuitive as the usage of both other options above, but it can be a life-saver when you have to parse large documents (as it does not need to load the whole document into memory and operates on the XML stream). The nice thing is that it allows you to inter-operate with the DOM via XMLReader::expand().
  • XML Parser is a very low-level component which allows you to create SAX parsers, which means that you define handler functions which will be called when reading the XML file; essentially they have the same benefits as the XMLReader (operating on streams)

My personal favorites are:

  • SimpleXML when parsing relatively small XML files without the need to modifiy them
  • XMLReader when parsing large XML files

用 PHP 解析 XML

用 PHP 读取和编写 XML的简单例子

http://www.ibm.com/developerworks/cn/opensource/os-xmldomphp/

PHP所使用的解析XML的库 C版本

http://sourceforge.net/projects/expat/

The Expat XML Parser

http://www.libexpat.org/

Expat is an XML parser library written in C. It is a stream-oriented parser in which an application registers handlers for things the parser might find in the XML document (like start tags).

PHPbuilder 关于PHP解析XML详细的解释和例子

http://www.phpbuilder.com/columns/justin20000428.php3

讲XML转换为数组格式的函数 xml_parse_into_struct

http://phpbuilder.com/manual/en/function.xml-parse-into-struct.php