Several methods for PHP to read XML-php Tutorial

Source: Internet
Author: User
Tags php regular expression
Several methods for PHP to read XML

  1. Zhang Ying
  2. Male
  3. 28
  4. Tank
  5. Male
  6. 28

1) DOMDocument reads xml

  1. $ Doc = new DOMDocument ();
  2. $ Doc-> load ('person. XML'); // read the xml file
  3. $ Humans = $ doc-> getElementsByTagName_r ("humans"); // get the object array of the humans tag
  4. Foreach ($ humans as $ human)
  5. {
  6. $ Names = $ human-> getElementsByTagName_r ("name"); // an array of objects that obtain the name tag
  7. $ Name = $ names-> item (0)-> nodeValue; // obtain the value in node, as shown in figure
  8. $ Sexs = $ human-> getElementsByTagName_r ("sex ");
  9. $ Sex = $ sexs-> item (0)-> nodeValue;
  10. $ Olds = $ human-> getElementsByTagName_r ("old ");
  11. $ Old = $ olds-> item (0)-> nodeValue;
  12. Echo "$ name-$ sex-$ old \ n ";
  13. }
  14. ?>

2) simplexml reading xml

  1. $ Xml_array = simplexml_load_file ('person. XML'); // read the data in xml to the array object.
  2. Foreach ($ xml_array as $ tmp ){
  3. Echo $ tmp-> name. "-". $ tmp-> sex. "-". $ tmp-> old ."
    ";
  4. }
  5. ?>

3) use a php regular expression to record data

  1. $ Xml = "";
  2. $ F = fopen ('person. XML', 'r ');
  3. While ($ data = fread ($ f, 4096 )){
  4. $ Xml. = $ data;
  5. }
  6. Fclose ($ f );
  7. // Read data above
  8. Preg_match_all ("/\ (.*?) \ <\/Humans \>/s ", $ xml, $ humans); // match the content in the outermost tag
  9. Foreach ($ humans [1] as $ k => $ human)
  10. {
  11. Preg_match_all ("/\ (.*?) \ <\/Name \>/", $ human, $ name); // match the name
  12. Preg_match_all ("/\ (.*?) \ <\/Sex \>/", $ human, $ sex); // match the gender
  13. Preg_match_all ("/\ (.*?) \ <\/Old \>/", $ human, $ old); // match the age
  14. }
  15. Foreach ($ name [1] as $ key => $ val ){
  16. Echo $ val. "-". $ sex [$ key] [1]. "-". $ old [$ key] [1]."
    ";
  17. }
  18. ?>

4) xmlreader reads xml data

  1. $ Reader = new XMLReader ();
  2. $ Reader-> open ('person. XML'); // read xml data
  3. $ I = 1;
  4. While ($ reader-> read () {// whether to read
  5. If ($ reader-> nodeType = XMLReader: TEXT) {// judge the node type
  6. If ($ I % 3 ){
  7. Echo $ reader-> value; // Get the node value
  8. } Else {
  9. Echo $ reader-> value ."
    ";
  10. }
  11. $ I ++;
  12. }
  13. }
  14. ?>

3. there are many methods to read xml. The above four methods can read the data in the tag and display it. 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 focuses on reading the values in tags and reading the attributes of tags to transmit data, all are placed in the attribute (but the method I wrote above still takes the value in the tag, because the xml file has been given, and I don't want to get the xml file out ).

For example,Xmlreader is designed to read the name sex old value in data, and reading content is troublesome. It is equivalent to attr (") in jquery.

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.