Use PHP5 to easily parse XML

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! 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 several 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 good, on the basis of which most of the requests can be processed.

Efficacy:
1 \ query, add, modify, and delete nodes in the basic XML file.
2 \ export all the data of the XML file to an array.
3 \ all designs adopt the OO method. when holding the result set, the application method is similar to dom

Faults:
1 \ each node should have an id (see the example below), and 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 throughPHP5And cannot run normally in PHP4.

The documents were not packed up because they have just been written,The example below demonstratesThe 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



  Hualian
Changan Street, Beijing-9999
  Supermarket chains
 

Food11

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.