Use PHP5.0 to easily parse XML documents

Source: Internet
Author: User
When using the sax method, you need to build three functions by yourself, and directly use these three functions to return data, requiring strong logic. When processing XML with different structures, we need to re-construct these three functions, which is troublesome! It is better to use the dom method,

When using the sax method, you need to build three functions by yourself, and directly use these three functions to return data, requiring strong logic. When processing XML with different structures, we need to re-construct these three functions, which is troublesome!

It is better to use the dom method, but he regards every node as a node and needs to write a lot of code to control it, which is troublesome!

There are a lot of open-source xml parsing class libraries on the internet. I have seen a few libraries before, but I always feel uneasy and always follow others' ass.

I have been engaged in Java these days, so I decided to change my head and write some PHP code. in order to prevent the xml parsing process from making me more difficult, it took a day to write the following xml parsing class, so we had the following.

The implementation method is implemented by packaging the 'Analysis result of the sax method'. In general, it is quite practical for me, and the performance is also acceptable. on the basis of this, most requests can be processed.

Efficacy:

1. query, add, modify, and delete the nodes of the basic xml file.

2. export all the data of the xml file to an array.

3. all designs adopt the oo method. when controlling the result set, the application method is similar to dom.

Faults:

1. it is recommended that each node have an id (see the example below). each "node name" = "node label _ node id". if this id value is not set, the program will take the initiative to generate an id for him. this id is the node's position number in his superior node, starting from 0.

2. when querying a node, you can use the "|" symbol to connect to the "node name. These "node names" are the names of upper-level nodes written in order.

Application clarification:

Run the following example and you can see the function application clarification on the fulfillment result page.

The code is implemented through php5 and cannot run properly in php4.

Since I have just finished writing the document, the following example demonstrates only some of the functions and the code is not very difficult. if you want to know more about the functions, you can study the source code.

Directory structure:

Test. php

Test. xml

Xml/SimpleDocumentBase. php

Xml/SimpleDocumentNode. php

Xml/SimpleDocumentRoot. php

Xml/SimpleDocumentParser. php

File: test. xml

<? Xml version = '1. 0' encoding = 'gb2312 '?> <Shop> <name> Hualian </name> <address> No. 9999, Chang'an Street, Beijing </address> <desc> supermarket chains </desc> <cat id = 'food'> <goods id = 'food11'> <name> food11 </name> <price> 12.90 </price> </goods> <goods id = 'food12'> <name> food12 </name> <price> 22.10 </price> <desc creator = 'hahawen'> recommended items </desc> </goods> </cat> <goods id = 'tel21'> <name> tel21 </name> <price> 1290 </price> </goods> </cat> <cat id = 'coat'> <goods id = 'coat31'> <name> coat31 </name> <price> 112 </price> </goods> <goods id = 'coat32'> <name> coat32 </ name> <price> 45 </price> </goods> </cat> <special id = 'Hot '> <goods> <name> hot41 </name> <price> 99 </price> </goods> </special> </shop>

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.