Example Analysis of parsing XML documents in PHP4 and PHP5 versions, php4php5

Source: Internet
Author: User
Tags php website xml parser

Example Analysis of parsing XML documents in PHP4 and PHP5 versions, php4php5

This document describes how to parse XML documents in PHP4 and PHP5. We will share this with you for your reference. The details are as follows:

During PHP website development and construction, you often encounter the need to parse XML documents. PHP4 comes with the XML Parser (sax), and PHP5 adds SimpleXML (dom-based) XML extension for XML parsing is even more convenient. Today, I would like to share with you how to parse XML documents in different environments.

XML document

<? Xml version = "1.0" encoding = "gbk"?> <LeapsoulXML> <LeapsoulInfo> <name> Leapsoul-PHP website development </name> <website> http://www.bkjia.com </website> <description> share the fun of PHP website development and construction, how to create a website </description> <bloger> David </bloger> <date> </date> <qq> QQ: 154130270 </qq> </LeapsoulInfo> <name> Leapsoul-PHP website development </name> <website> http://www.bkjia.com </website> <description> share PHP website development and construction fun, how to create a website </description> <bloger> David </bloger> <date> </date> <qq> QQ: 154130270 </qq> </LeapsoulInfo> </LeapsoulXML>

Use SimpleXML in PHP5

$info=simplexml_load_file('leapsoulcn.xml');print_r($info);$name=$info->LeapsoulInfo[0]->name;echo$name;foreach($info->LeapsoulInfoas$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 />';}$info->LeapsoulInfo[0]->addChild('msn','MSN:davidfaithman@hotmail.com');$info->asXML('leapsoulcn.xml');

Code comment

Row 1st: simplexml_load_file: reads an xml document as the operation object and can read local or remote xml documents. simplexml_load_string: reads an xml string as the operation object.
Row 3rd: if you do not know how to obtain information about a node, you can use the print_r function to print the output to view the specific structure. The objects returned by simplexml parsing have an array structure.
5th ~ Row 8: Read the node information of an XML document as an object. Read method: handle-> node element name-> subnode. If there are multiple identical node elements, read in array Mode

Note: Because simplexml parses the returned information in UTF8 format, if the website uses GBK, transcoding is required. You can use the iconv function or other utf8 and gbk conversion functions to perform operations, for example: $ name = iconv ('utf-8', 'gbk', $ name );

9th ~ Row 17: reads the subnode information of all elements in the form of traversal.
19th ~ Row 21: The xpath function of simplexml is used to query XML data. For example, the value of all name nodes is queried.
23rd ~ Row 26: The children function is used to find the values of all subnodes under a specific node. The getName function is used to obtain the element names of each subnode.
28th ~ Row 29: The addChild function is used to add a subnode to a specific node. The asXML function saves modified XML documents.

Click to view more SimpleXML function descriptions.

XML function parsing method provided by PHP4

Function doStartElement ($ xmlParser, $ name, $ attr) {// process the Start Element} function doEndElement ($ xmlParser, $ name) {// processing of end elements} function doStringData ($ xmlParser, $ data) {// processing of character data between elements} $ xmlObj = xml_parser_create ("UTF-8 "); xml_set_element_handler ($ xmlObj, "doStartElement", "doEndElement"); handler ($ xmlObj, "doStringData"); xml_parse ($ xmlObj, file_get_contents ("leapsoulcn. xml "); xml_parser_free ($ xmlObj );

Code comment

1st ~ Row 15: defines the processing functions of the starting element, ending element, and character data between elements.
Row 16th: Create a New XML Parser and return the resource handle that can be used by other XML functions. The input is encoded by default as ISO-8859-1 ". The supported encoding methods are UTF-8 and US-ASCII, and the output data encoding method is consistent with the definition at the xml_parser_create function.
Row 3: Creates a starting and ending element processor.
Row 3: create a character data processor to process related data between elements
Note: The XML parser does not add or remove any spaces. The choice of spaces is determined by the developer.
Row 22nd: The second parameter of the xml_parse function of the Start parser is in bytes type. Therefore, you need to read the relevant XML document information. Here, I use the file_get_contents function, which is feasible for local remote applications, you can also use the fopen function.
Row 3: Release the memory occupied by the parser after the XML parsing is complete.

Built-in XML function parsing ideas

Step 1: Customize the processing functions of the starting element, ending element, and character data between elements;
Step 2: Create an XML parser;
Step 3: Create a starting and ending element processor;
Step 4: create a character data processor to process related data between elements;
Step 5: Start the parser;
Step 6: Release the memory occupied by the parser after the XML parsing is complete.

Summary:

In general, the XML Parser of PHP is quite tedious to use and requires strong logic. If the XML file structure is different, the three functions, such as elements and characters, need to be redefined, with SimpleXML extension added in PHP5, XML parsing is very simple. If you need to perform more complex XML document parsing operations, you can also download and install libxml, powerful functions.

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.