In the development and construction of PHP Web site, often encounter the need to parse XML documents, PHP4 version of the XML parser (sax), PHP5 version of the increase of simplexml (DOM) of XML extensions, XML parsing is very convenient, Share with you today the method of parsing XML documents in different environments.
XML Document
1 2 3 4 5 6 7 8 9 Ten One |
<?xml version=, " " " , | 1.0 "encoding=" GBK " <LeapsoulXML> <LeapsoulInfo> <name>leapsoul-php website Development </name> <website>http:// Www.leapsoul.cn</website> <description> share the fun of PHP website development and construction, teach you how to build a website </description> <BLOGER>DAVID</BLOGER> <date>2009-05-13</date> <QQ>QQ:154130270</QQ> </LEAPSOULINFO> <LeapsoulInfo> <name& Gt leapsoul-php website Development </name> &NBSP <website>http://www.leapsoul.cn</website> <description> Share the fun of PHP website development and construction, teach you how to build a website </description> <BLOGER>DAVID</BLOGER> <DATE>2009-05-13</DATE> <qq>qq:154130270& Lt;/qq> </leapsoulinfo> </leapsoulxml>
simplexml usage under PHP5 version
1 2 3 4 5 6 7 8 9 Ten One " " " " " " " " [ ] [ ] |
$info = simplexml_load_file (' leapsoulcn.xml '); Print_r ($info); $name = $info->leapsoulinfo[0]->name; Echo $name; foreach ($info->leapsoulinfo as $LeapsoulInfo) { echo $LeapsoulInfo->name. " <br/> "; Echo $LeapsoulInfo->website. " <br/> "; Echo $LeapsoulInfo->description. " <br/> "; Echo $LeapsoulInfo->bloger. " <br/> "; Echo $LeapsoulInfo->date. " <br/> "; Echo $LeapsoulInfo->qq. " <br/> "; } foreach ($info->xpath ('//name ') as $value) { echo $value. ' <BR/≫ '; } foreach ($info->leapsoulinfo[0]->children () as $value) { Echo $value->getname (); echo $value. ' <br/> '; &NBSP } $info->leapsoulinfo[0]->addchild (' MSN ', ' msn:davidfaithman@hotmail.com '); $info- >asxml (' Leapsoulcn.xml '); |
Code comments
Line 1th:simplexml_load_file: Reads an XML document as an Action object, can read a local or remote XML document;simplexml_load_string : Reads an XML string as an object of operation
Line 3rd: If you do not know how to get information about a node, you can use the Print_r function printout to view the specific structure, simplexml the returned object has an array structure.
Line 5th to 8th: read an XML document node information in an object way: Handle-> node element name-> child node, if the same node element has more than one, then read as array
Note : Because the SimpleXML parsing returns the information is UTF8 format, if the website uses is GBK, then needs the transcoding, you may use the Iconv function or other UTF8 and the GBK conversion function to operate, for example:$name = Iconv (' utf-8′, ' GBK ', $name);
Line 9th to 17th: Read the child node information under all the elements in the form of traversal
Line 19th to 21st: simplexml XPath functions are used to query XML data, such as the value of all name nodes queried here
Line 23rd to 26th: Thechildren function is used to find values for all child nodes under a particular node. The getName function is used to get the element name of each child node
Line 28th to 29th: TheaddChild function is used to add a child node under a particular node; The asxml function saves the XML document that has been altered
Click to see more simplexml function descriptions.
XML function parsing method with PHP4 version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24
|
function Dostartelement ($xmlParser, $name, $attr) { Handling of the Start element }
function Doendelement ($xmlParser, $name) { Processing of the end element }
function Dostringdata ($xmlParser, $data) { Processing of character data between elements }
$XMLOBJ = Xml_parser_create ("UTF-8");
Xml_set_element_handler ($XMLOBJ, "dostartelement", "doendelement");
Xml_set_character_data_handler ($XMLOBJ, "dostringdata");
Xml_parse ($XMLOBJ, file_get_contents ("Leapsoulcn.xml"));
Xml_parser_free ($XMLOBJ); |
Code comments
Line 1th to 15th: Defines the start element, the end element, and the processing function of the character data between elements
Line 16th: Create a new XML parser and return a resource handle that can be used by other XML functions, and enter the default encoding as "Iso-8859-1". At the same time, the supported coding methods are "UTF-8" and "Us-ascii", and the output data encoding method is consistent with the definition of the Xml_parser_create function.
Line 18th: Establish start and end element processors
Line 20th: Create a character data processor to handle data between elements
Note : The XML parser will not add or remove any spaces, and the choice of spaces will be decided by the developer.
Line 22nd: Start parser Xml_parse function The second argument is character type, so you need to read the relevant XML document information, where I use the file_get_contents function, local remote is feasible, of course, you can also use the fopen function
Line 24th: Release the memory consumed by the parser after XML parsing is complete
The idea of XML function parsing with self
The first step is to customize the start element, the end element, and the processing function of the character data between elements; Step two: Create an XML parser; step three: Set up the starting and terminating element processor; Step Fourth: Establish character data processor, process the related data between elements; Step Fifth: Start the parser Sixth step: After XML parsing is complete, release the memory occupied by the parser.
Summary :
In general, PHP's own XML parser is very cumbersome to use, need strong logic, and if the structure of XML documents, elements, characters and other 3 functions need to be redefined, and in the PHP5 version of the simplexml extension, XML parsing is very simple, Of course, if you need more complex XML document parsing operations, you can download the installation and use Libxml, the function is quite strong.
Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.