- $ Simple =" Simple note ";
- $ P = xml_parser_create ();
- Xml_parse_into_struct ($ p, $ simple, $ vals, $ index );
- Xml_parser_free ($ p );
- Echo "Index array \ n ";
- Print_r ($ index );
- Echo "\ nVals array \ n ";
- Print_r ($ vals );
- ?>
-
Output:
$ Xml ='
-
- Note1 Extra1
- Note2
- Note3 Extra3
- ';
$ P = xml_parser_create ();
- Xml_parse_into_struct ($ p, $ xml, $ values, $ tags );
- Xml_parser_free ($ p );
- $ Result = array ();
- // The following traversal method has bugs
- For ($ I = 0; $ I <3; $ I ++ ){
- $ Result [$ I] = array ();
- $ Result [$ I] ["note"] = $ values [$ tags ["NOTE"] [$ I] ["value"];
- $ Result [$ I] ["extra"] = $ values [$ tags ["EXTRA"] [$ I] ["value"];
- }
- Print_r ($ result );
- ?>
If we traverse through the above method, we will get the wrong result (extra3 ran to the second para ). Therefore, we need to traverse in a more rigorous way:
$ Result = array ();
- $ ParaTagIndexes = $ tags ['para'];
- $ ParaCount = count ($ paraTagIndexes );
- For ($ I = 0; $ I <$ paraCount; $ I + = 2 ){
- $ Para = array ();
- // Traverse all values between para tag pairs
- For ($ j = $ paraTagIndexes [$ I]; $ j <$ paraTagIndexes [$ I + 1]; $ j ++ ){
- $ Value = $ values [$ j] ['value'];
- If (empty ($ value) continue;
$ Tagname = strtolower ($ values [$ j] ['tag']);
- If (in_array ($ tagname, array ('note', 'Extra '))){
- $ Para [$ tagname] = $ value;
- }
- }
- $ Result [] = $ para;
- }
- ?>
-
In fact, the xml_parse_into_struct function is rarely used at ordinary times, so the above so-called "rigorous" code is not properly maintained and there will be bugs in other situations. Xml_set_element_handler This method sets a callback function for parser to process the start and end of an element. Xml_set_character_data_handler is used to set the data callback function for parser. The code written in this way is clear and easy to maintain. Example:
$ Xml = <
- Note1 Extra1
- Note2
- Note3 Extra3
- XML;
$ Result = array ();
- $ Index =-1;
- $ CurrData;
Function charactor ($ parser, $ data ){
- Global $ currData;
- $ CurrData = $ data;
- }
Function startElement ($ parser, $ name, $ attribs ){
- Global $ result, $ index;
- $ Name = strtolower ($ name );
- If ($ name = 'para '){
- $ Index ++;
- $ Result [$ index] = array ();
- }
- }
Function endElement ($ parser, $ name ){
- Global $ result, $ index, $ currData;
- $ Name = strtolower ($ name );
- If ($ name = 'note' | $ name = 'Extra '){
- $ Result [$ index] [$ name] = $ currData;
- }
- }
$ Xml_parser = xml_parser_create ();
- Xml_set_character_data_handler ($ xml_parser, "charactor ");
- Xml_set_element_handler ($ xml_parser, "startElement", "endElement ");
- If (! Xml_parse ($ xml_parser, $ xml )){
- Echo "Error when parse xml :";
- Echo xml_error_string (xml_get_error_code ($ xml_parser ));
- }
- Xml_parser_free ($ xml_parser );
Print_r ($ result );
- ?>
It can be seen that although the set handler method has many lines of code, its ideas are clear and its readability is better, but its performance is slightly slower than the first method, and its flexibility is not strong. XML Parser supports PHP4 and is suitable for systems of earlier versions. For the PHP5 environment, consider the following methods first. 2, SimpleXML SimpleXML is a simple and easy-to-use xml tool set provided by PHP5. it can convert xml into easy-to-process objects and organize and generate xml data. However, it does not apply to xml that contains namespace, and must ensure that the xml format is complete (well-formed ). It provides three methods: simplexml_import_dom, simplexml_load_file, and simplexml_load_string. the function name intuitively illustrates the function's role. All three functions return SimpleXMLElement objects, and data reading/adding is performed through SimpleXMLElement.
$ String = <
-
- Login
- Imdonkey
- XML;
$ Xml = simplexml_load_string ($ string );
- Print_r ($ xml );
- $ Login = $ xml-> login; // The Returned result is still a SimpleXMLElement object.
- Print_r ($ login );
- $ Login = (string) $ xml-> login; // when comparing data, be sure to forcibly convert
- Print_r ($ login );
- ?>
SimpleXML is easy to develop. Its disadvantage is that it loads the entire xml into the memory before processing. Therefore, it may not be able to resolve xml documents with too much content. If you want to read small files and xml does not contain namespace, SimpleXML is a good choice. 3. XMLReaderXMLReader is also an extension after PHP5 (installed by default after 5.1). It moves in the document stream just like a cursor and stops at each node. The operation is flexible. It provides fast and non-cache stream access to the input, and can read streams or documents, allowing users to extract data from it and skip records that are meaningless to the application.12. Next page |