Home >Backend Development >PHP Tutorial >Several ways to read XML with PHP

Several ways to read XML with PHP

WBOY
WBOYOriginal
2016-07-25 09:10:331399browse
  1. Zhang Ying
  2. Male
  3. 28
  4. tank
  5. Male
  6. < ;old>28
Copy code

1) DOMDocument reads xml

  1. $doc = new DOMDocument();
  2. $doc->load('person.xml'); //Read 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" ); //Get the object array of the tag name
  7. $name = $names->item(0)->nodeValue; //Get the value in node, such as
  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 - $oldn";
  13. }
  14. ?>
Copy code

2) simplexml reads xml

  1. $xml_array=simplexml_load_file('person.xml'); //Read the data in XML into an array object
  2. foreach($xml_array as $tmp){
  3. echo $ tmp->name."-".$tmp->sex."-".$tmp->old."
    ";
  4. }
  5. ?>
Copy code

3 ) Use php regular expressions to remember data

  1. $xml = "";
  2. $f = fopen('person.xml', 'r');
  3. while( $data = fread( $f, 4096 ) ) {
  4. $xml .= $data;
  5. }
  6. fclose( $f );
  7. // Read the data above
  8. preg_match_all( "/(.*?)/s", $xml, $ humans ); //Match the content in the outermost tag
  9. foreach( $humans[1] as $k=>$human )
  10. {
  11. preg_match_all( "/(.*?)
  12. preg_match_all( "/(.*?)/", $human, $sex ); //Match the gender
  13. preg_match_all( "/(.*?)/", $human, $old ); //Match age
  14. }
  15. foreach($name[1] as $key=> $val){
  16. echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."
    " ;
  17. }
  18. ?>
Copy code

4) xmlreader to read 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) { //Determine the node type
  6. if($i%3){
  7. echo $reader- >value; //Get the value of node
  8. }else{
  9. echo $reader->value."
    " ;
  10. }
  11. $i++;
  12. }
  13. }
  14. ?>
Copy code

Three, summary There are many ways to read xml, just to name a few. The above four methods can all read the data in the tag, Zhang Ying. But their testing focus is different. The design focus of the function of reading xml in the first three methods is to read the value in the tag, which is equivalent to The text() method in jquery is different from xmlreader. Its focus is not on reading the value in the tag, but on reading the attributes of the tag and putting all the data to be transmitted in the attributes (but as I mentioned above The method I wrote still takes the value in the tag, because the xml file has been given, and I don’t want to create the xml file again).

Give me an example to explain, The design focus of xmlreader is to read the value of name sex old in data, but reading the content is more troublesome. It is equivalent to attr(”); in jquery.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn