There are a lot of classes or plug-ins for processing xml documents in php. today I will introduce you to xmlreadersimplexmlDOMDocument and other xml reading examples. I hope the following will help you. to process XML files, there are... there are a lot of classes or plug-ins for processing xml documents in php. today I will introduce you to xmlreader simplexmlDOMDocument and other xml reading examples. I hope the following will help you.
There are two traditional methods to process XML files: SAX, DOM, and SAX. based on the event trigger mechanism, they perform a scan of XML files to complete the processing, DOM constructs the entire XML file as a DOM tree and completes processing by traversing the DOM tree. These two methods have their own advantages and disadvantages. the processing logic of SAX is relatively abstract, the DOM processing process is cumbersome and is not suitable for beginners. PHP5 has introduced a new set of XML processing functions, namely SimpleXML. In fact, SimpleXML itself is compact and provides only a few method functions, however, the XML file processing function is very powerful and easy to operate.
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 common Markup Language ). Xml is cross-platform in the Internet environment and relies on content technology. it 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 conveniently 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
The xml source file 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:
$ Xml_array = simplexml_load_file ('person. XML'); // read the data in xml to the array object foreach ($ xml_array as $ tmp) {echo $ tmp-> name. "-". $ tmp-> sex. "-". $ tmp-> old."
";}
3) use a php regular expression to retrieve data. the code is as follows:
$ Xml = ""; $ f = fopen ('person. XML', 'r'); while ($ data = fread ($ f, 4096) {$ xml. = $ data;} fclose ($ f); // read data preg_match_all ("/\
(.*?) \ <\/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:
$ Reader = new XMLReader (); $ reader-> 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 ++ ;}}
Use DOMDocument to manipulate XML
1. DOMDocument parses XML and the code is as follows:
// Create a DOMDocument () $ _ doc = new DOMDocument (); // load xml $ _ doc-> load ('test. xml '); // Obtain the version label $ _ version =$ _ doc-> getElementsByTagName ('version'); echo $ _ version-> item (2)-> nodeValue; // traverse the version label foreach ($ _ version as $ v) {echo $ v-> nodeValue ;}
2. DOMDocument generates XML
// Declare xml $ _ doc = new DOMDocument ('1. 0', 'utf-8'); // typographical format $ _ doc-> formatOutput = true; // create a primary tag $ _ root =$ _ doc-> createElement ('root '); // create a first-level label version $ _ version =$ _ doc-> createElement ('version '); // assign $ _ versionTextNode =$ _ doc-> createTextNode ('1. 0 '); // add the value to the version label $ _ version-> appendChild ($ _ versionTextNode ); // put the first-level label version in root $ _ root-> appendChild ($ _ version); // write the master label into xml $ _ doc-> appendChild ($ _ root ); // generate xml $ _ doc-> save ('AAA. XML ');
In many cases, manually generating tags requires that documents be generated from top to bottom. you must ensure that the tags are complete. the start and end tags can be improved by using some PHP functions or classes, however, PHP also provides a set of more helpful built-in objects and functions, including the Document Object Model, Document Object Model, and DOM. It provides a tree structure that allows you to easily create and process tags.
III. Summary
There are many ways to read xml. for example, the above four methods can read the data in tags, Zhang Ying . However, their test focus is different. the design focus of the function reading xml in the first three methods is to read the values in the tag, which is equivalent to the text () method in jquery, xmlreader is not the same as xmlreader. Instead, xmlreader does not focus on reading the values in Tags. Instead, it reads the attributes of tags and stores the data to be transmitted in attributes, but the method I wrote above still gets the value in the tag, because the xml file has been given, and I don't want to get the xml file out. let's explain it as an example.
Xmlreader is designed to read the name sex old value in data.The content is quite troublesome. it is equivalent to attr ('') in jquery.
Address:
Reprinted at will, but please attach the article address :-)