Methods of using PHP to operate XML files using the sax and simplexml Methods

Source: Internet
Author: User
Tags xml parser

1. Sax, that is, simpleAPIForSimple Application Interface of XML and XML

       Specific steps: & #9312; create an XML Parser and use the xml_parser_create function. The parameter is encoded and the return value is the corresponding parser resource.

                        & #9313; create a resolution handler to process the start tag, end tag, and data respectively, and set the tag handler to xml_set_element_handler. The first parameter is the parser resource, the second is the function name for processing the start tag, and the second is the function name for processing the end tag. The function for processing data is set to xml_set_character_data_handler. The first parameter is the parser resource, the second is the data processing function name. The three custom functions defined here need to be implemented by yourself.

                        & #9314; Start parsing, xml_parse, three parameters. The first is the parser resource, and the second is the content to be processed. The content here must be in the string format, therefore, you can use the file_get_contents function to read the string obtained from the file. If the third parameter is set to true, it indicates that the parser is the last data, otherwise, the parser will continue to wait and will not consider it as processing completed. Therefore, to get an error, you need to set it to true. After the parser completes processing, it will know that there is no data to be processed, you can determine whether there are any errors and throw them.

                        & #9315; how to handle an error? xml_get_error_code, xml_get_current_line_number, xml_get_current_column_number are the columns that obtain the error code, the row where the error is located, and the parameters are all parser resources, there is also an xml_error_string to get the error message. This parameter is a bit different. The returned value of xml_get_error_code is used as the parameter.

       Advantages and disadvantages of this method (relative to the DOM method ):

       & #9312; Dom needs to read all data to the memory and process the node before performing the operation. Sax reads one, operates one, and releases one

       & #9313; when the XML document structure is incorrect, Dom cannot be parsed into a DOM tree, while sax can be operated until an error occurs.

       & #9314; sax can only read data, while Dom can do a lot of complex operations. It is suitable for large documents with fast processing speed.

Ii. simplexml, which is a simple XML processing method provided by PHP. It forms an object set in the memory of XML data and converts objects of the same level into arrays, easy to operate

       Step: & #9312; instantiate a simplexmlelement object. The parameter is a string obtained through file_get_contents. The obtained object is equivalent to the root element of the XML document, $ sxe=NewSimplexmlelement ($ string );

                  & #9313; access the node $ sxe-> book-> title;. to access a node, direct the access to the next subnode through the root node and direct the access to the node, to obtain the Node object, you can directly use echo to output the data in the node, because the object has a method similar to _ tostring and can also be accessed in an array, $ sxe-> book [2]-> title, or directly traverse foreach ($ sxe-> bookAs$ Book) {echo$ Book-> title}

                  & #9314; add node $ sxe-> addchild ('book ','Abcd'). The parameters are the node name and node value. This method can be used to add subnodes to any node.

                  & #9315; add attributes: $ book-> addattribute ('id ','Book10 ')

                  & #9316; delete a node: unset ($ sxe-> book [2]);

                  & #9317; update node: assign a value directly after finding the node

                  & #9315; Save the XML file: $ sxe-> asxml ('book. xml ');The parameter is the file name.

Mutual conversion between simplexmlelement object and DOM object:

1. Convert Dom to simplexmlelement: use the $ sxe function directly.=Simple_import_dom ($ DOC );$ Doc is the DOM object to be converted.

2. Convert simplexmlelement to a DOM object: $ Root=Dom_import_simplexml ($ sxe); now we only get the node, because $ sxe itself is equivalent to the root Element Object of the XML document, so we also need to create a DOM tree, then contact the node and tree: & #9312; $ Doc=NewDomdocument ('1. 0 ','Utf-8 ');  & Amp; #9313; $ domelement=$ Doc-> importnode ($ root,True), true is very important here. If this parameter is not set, the root Element Node object is put on the DOM tree. True indicates recursively putting all nodes on the DOM tree,& Amp; #9314; $ doc-> appendchild ($ domelement ); & #9315; $ doc-> Save ('book. xml ');

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.