How does PHP read xml files? Many of my friends do not know this problem. In fact, php has special functions for the xml document. let's take a few examples of php reading xml, hoping to help you. i. What is xml? what is xml used... how does PHP read xml files? Many of my friends do not know this problem. In fact, php has special functions for the xml document. let's take a few examples of php reading xml, hoping to help you.
I. What is xml? what is xml used?
XML (Extensible Markup Language) can be used to expand the Markup Language. like HTML, it is SGML (Standard Generalized Markup Language, Standard generic Markup Language). Xml is cross-platform in the Internet environment, content-based technology is a powerful tool for processing structured document information. XML is a simple data storage language that uses a series of simple tags to describe data, these tags can be easily created. Although XML occupies more space than binary data, XML is extremely easy to master and use.
XML is widely used to store data, exchange data, and prompt data for many applications.
2. how to read xml from php
Xml source file, the code is as follows:
Zhang Ying
Male
28
Tank
Male
28
1) DOMDocument reads xml and the code is as follows:
Load ('person. XML'); // read the xml file $ humans = $ doc-> getElementsByTagName ("humans"); // Obtain the object array foreach ($ humans as $ human) of the humans tag) {$ names = $ human-> getElementsByTagName ("name"); // array of objects for obtaining the name tag $ name = $ names-> item (0)-> nodeValue; // obtain the value in the node, as shown in figure
$ Sexs = $ human-> getElementsByTagName ("sex"); $ sex = $ sexs-> item (0)-> nodeValue; $ olds = $ human-> getElementsByTagName ("old"); $ old = $ olds-> item (0)-> nodeValue; echo "$ name-$ sex-$ old \ n" ;}?>
2) simplexml:
Name. "-". $ tmp-> sex. "-". $ tmp-> old ."
";}?>
If you use curl to obtain xml data
$ Xml = simplexml_load_string ($ data );
$ Data ['TK '] = json_decode (json_encode ($ xml), TRUE );
If you directly obtain URL data
$ Xml = simplexml_load_file ($ data );
$ Data ['TK '] = json_decode (json_encode ($ xml), TRUE );
Convert simplexml to json and then convert json to an array.
3) use a php regular expression to retrieve data. the code is as follows:
(.*?) \ <\/Humans \>/s ", $ xml, $ humans ); // match the content of foreach ($ humans [1] as $ k => $ human) {preg_match_all ("/\
(.*?) \ <\/Name \>/", $ human, $ name); // match the name preg_match_all ("/\
(.*?) \ <\/Sex \>/", $ human, $ sex); // match the gender preg_match_all ("/\
(.*?) \ <\/Old \>/", $ human, $ old); // match the age} foreach ($ name [1] as $ key => $ val) {echo $ val. "-". $ sex [$ key] [1]. "-". $ old [$ key] [1]."
";}?> 4) xmlreader reads xml data. the code is as follows:
Open ('person. XML'); // read xml data $ I = 1; while ($ reader-> read () {// whether to read if ($ reader-> nodeType = XMLReader:: TEXT) {// Determine the node type if ($ I % 3) {echo $ reader-> value; // Obtain the node value} else {echo $ reader-> value."
";}$ I ++ ;}}?>
III. Summary
There are many ways to read xml, just a few simple examples. the above four methods can read the data in the tag, but their test focus is different, the design focus of the function for reading xml in the first three methods is to read the value in the tag, which is equivalent to the text () method in jquery, and xmlreader is not the same, the key is not to read the values in the tag, but to read the attributes of the tag and put all the data to be transferred in the attribute. However, the method I wrote above still takes the values in the tag, because the xml file has been given, I don't want to create an xml file.
The code is as follows:
Xmlreader is designed to read the name sex old value in data, which is equivalent to attr ('') in jquery.
Article address:
Reprint at will ^ please include the address of this article!