Using PHP5.0 to easily parse XML documents (1) when using the sax method, you need to build three functions by yourself and use these three functions to directly return data, which requires strong logic. When processing xml with different structures and using the sax method, you need to build three functions by yourself and use these three functions to directly return data, logic with strong requirements. 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. it is troublesome to write a lot of code for operations!
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 that I am not steadfast, and I feel that I am always behind 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. basically, most of the processing requirements can be fulfilled.
Function:
1. query, add, modify, and delete nodes of basic xml files.
2. export all the data of the xml file to an array.
3. the entire design adopts the oo method. when operating the result set, the method used is similar to dom.
Disadvantages:
1. it is recommended that each node 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 automatically generate an id for him, which 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.
Instructions for use:
Run the following example and you can see the function instructions on the execution result page.
The code is implemented through php5 and cannot run properly in php4.
As I have just finished writing the documents, the following example demonstrates only some functions and the code is not very difficult. if you want to know more 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> Beijing Chang'an Street-No. 9999 </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>
<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>
File: test. php
<? Php require_once "xml/SimpleDocumentParser. php ";
Require_once "xml/SimpleDocumentBase. php ";
Require_once "xml/SimpleDocumentRoot. php ";
Require_once "xml/SimpleDocumentNode. php ";
$ Test = new SimpleDocumentParser ();
$ Test-> parse ("test. xml ");
$ Dom = $ test-> getSimpleDocument ();
Echo "<pre> ";
Echo "Echo "the following is an array of the entire xml data returned by the getSaveData () function ";
Echo "</font> Print_r ($ dom-> getSaveData ());
Echo "Echo "the following uses the setValue () function to add information to the root node. after adding the information, the content of the result xml file is displayed ";
Echo "</font> $ Dom-> setValue ("telphone", "123456789 ");
Echo htmlspecialchars ($ dom-> getSaveXml ());
Echo "Echo "the following uses the getNode () function to return information about all products under a certain category ";
Echo "</font> $ Obj = $ dom-> getNode ("cat_food ");
$ NodeList = $ obj-> getNode ();
Foreach ($ nodeList as $ node ){
$ Data = $ node-> getValue ();
Echo "<font color = red> product name:". $ data [name]. "</font> <br> ";
Print_R ($ data );
Print_R ($ node-> getAttribute ());
}
Echo "Echo "the following uses the findNodeByPath () function to return information about a product ";
Echo "</font> $ Obj = $ dom-> findNodeByPath ("cat_food | goods_food11 ");
If (! Is_object ($ obj )){
Echo "this item does not exist ";
} Else {
$ Data = $ obj-> getValue ();
Echo "<font color = red> product name:". $ data [name]. "</font> <br> ";
Print_R ($ data );
Print_R ($ obj-> getAttribute ());
}
Echo "Echo "the following uses the setValue () function to add properties to item \" food11 \ ", and then displays the added result ";
Echo "</font> $ Obj = $ dom-> findNodeByPath ("cat_food | goods_food11 ");
$ Obj-> setValue ("leaveword", array ("value" => "This item is good ",
"Attrs" => array ("author" => "hahawen", "date" => date ('Y-m-D '))));
Echo htmlspecialchars ($ dom-> getSaveXml ());
Echo "Echo "the following is the removeValue ()/removeAttribute () function,
Change and delete properties for item \ "food11 \", and then display the operation result ";
Echo "</font> $ Obj = $ dom-> findNodeByPath ("cat_food | goods_food12 ");
$ Obj-> setValue ("name", "new food12 ");
$ Obj-> removeValue ("desc ");
Echo htmlspecialchars ($ dom-> getSaveXml ());
Echo "Echo "The following describes how to use the createNode () function to add products and then display the added results ";
Echo "</font> $ Obj = $ dom-> findNodeByPath ("cat_food ");
$ NewObj = $ obj-> createNode ("goods", array ("id" => "food13 "));
$ NewObj-> setValue ("name", "food13 ");
$ NewObj-> setValue ("price", 100 );
Echo htmlspecialchars ($ dom-> getSaveXml ());
Echo "Echo "the removeNode () function is used to delete a product and then display the deleted result ";
Echo "</font> $ Obj = $ dom-> findNodeByPath ("cat_food ");
$ Obj-> removeNode ("goods_food12 ");
Echo htmlspecialchars ($ dom-> getSaveXml ());
?>