Php reading xml

Source: Internet
Author: User
Php has an example of the xml reading problem on the internet. the code is as follows, but it is not displayed when it is run. it only shows & quot; Name: & quot; and there is nothing behind it, do you still need to configure it? Please help solve it !! & Lt ;? Php $ parser & nbsp; xml_parser_create (); & nbsp; create a parser editor xml_set_e php to read xml
I gave an example on the internet. the code is as follows, but the code is not displayed. it only shows "name:" And there is nothing behind it. do you still need to configure it, please help solve this problem !!
$ Parser = xml_parser_create (); // Create a parser editor
Xml_set_element_handler ($ parser, "startElement", "endElement"); // corresponding functions for tag setting triggering are startElement and endElenment respectively.
Xml_set_character_data_handler ($ parser, "characterData"); // you can specify a function for reading data.
$ Xml_file = "1.xml"; // specify the xml file to be read, which can be a url
$ Filehandler = fopen ($ xml_file, "r"); // open the file




While ($ data = fread ($ filehandler, 4096 ))
{
Xml_parse ($ parser, $ data, feof ($ filehandler ));
} // Extract 4096 bytes each time for processing

Fclose ($ filehandler );
Xml_parser_free ($ parser); // Close and release the parser


$ Name = false;
$ Position = false;
Function startElement ($ parser_instance, $ element_name, $ attrs) // function of the start tag event
{
Global $ name, $ position;
If ($ element_name = "NAME ")
{
$ Name = true;
$ Position = false;
Echo "name :";
}
If ($ element_name = "POSITION ")
{$ Name = false;
$ Position = true;
Echo "position :";
}
}

Function characterData ($ parser_instance, $ xml_data) // function used to read data
{
Global $ name, $ position;
If ($ position)
Echo $ xml_data ."
";
If ($ name)
Echo $ xml_data ."
";
}

Function endElement ($ parser_instance, $ element_name) // function for ending a tag event
{
Global $ name, $ position;
$ Name = false;
$ Position = false;
}

?>



The xml file code is as follows:







Zhang San
Manager



Li Si
Assistant





------ Solution --------------------
There is an xml class that has been encrypted and can read any xml into an array, which is also implemented using PHP built-in functions. Send it to you
------ Solution --------------------
Assume that your XML is GB2312 encoded, and XML is UTF-8 by default. You can change your XML file to UTF8.
------ Solution --------------------
The source file is correct, and the test under PHP4 is correct.
------ Solution --------------------
The encoding is UTF-8. I don't know if the gb2312 environment has any influence.
------ Solution --------------------
Of course there is an impact. if it is UTF-8, you 'd better use it in gb2312 for transcoding.
------ Solution --------------------

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.