zoukankan      html  css  js  c++  java
  • 解析XML数据,必看

    xml源文件

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <humans>
    3.     <zhangying>
    4.         <name>张映</name>
    5.         <sex></sex>
    6.         <old>28</old>
    7.     </zhangying>
    8.     <tank>
    9.         <name>tank</name>
    10.         <sex></sex>
    11.         <old>28</old>
    12.     </tank>
    13. </humans>
    复制代码

    1)DOMDocument读取xml

    1. <?php
    2. $doc = new DOMDocument();
    3. $doc->load('person.xml'); //读取xml文件
    4. $humans = $doc->getElementsByTagName_r( "humans" ); //取得humans标签的对象数组
    5. foreach( $humans as $human )
    6. {
    7.     $names = $human->getElementsByTagName_r( "name" ); //取得name的标签的对象数组
    8.     $name = $names->item(0)->nodeValue; //取得node中的值,如<name> </name>
    9.     $sexs = $human->getElementsByTagName_r( "sex" );
    10.     $sex = $sexs->item(0)->nodeValue;
    11.     $olds = $human->getElementsByTagName_r( "old" );
    12.     $old = $olds->item(0)->nodeValue;
    13.     echo "$name - $sex - $oldn";
    14. }
    15. ?>
    <?php
     //创建一个DOMDocument对象
     $doc=new DOMDocument();
     //加载XML文件
     $doc->load("books.xml");
     //获取所有的book标签
     $bookDom=$doc->getElementsByTagName("book");
     foreach($bookDom as $book){
      $title = $book->getElementsByTagName("title")->item(0)->nodeValue;
      $author = $book->getElementsByTagName("author")->item(0)->nodeValue;
      $year = $book->getElementsByTagName("year")->item(0)->nodeValue;
      $price = $book->getElementsByTagName("price")->item(0)->nodeValue;
      echo "title:".$title."<br>";
      echo "author:".$author."<br>";
      echo "year:".$year."<br>";
      echo "price:".$price ."<br>";
      echo "***********************************<br>";
     }
    ?>

    单条就不必循环了:

    $dom->load($url1);
    $status = $dom->getElementsByTagName( "productStatusName" ); //取得productStatusName的标签的对象数组
    $status = $status->item(0)->nodeValue;

    复制代码

    2)simplexml读取xml

    1. <?php
    2. $xml_array=simplexml_load_file('person.xml'); //将XML中的数据,读取到数组对象中
    3.     foreach($xml_array as $tmp){
    4.     echo $tmp->name."-".$tmp->sex."-".$tmp->old."<br>";
    5. }
    6. ?>
    复制代码

    3)用php正则表达式来记取数据

    1. <?php
    2.     $xml = "";
    3.     $f = fopen('person.xml', 'r');
    4.     while( $data = fread( $f, 4096 ) ) {
    5.         $xml .= $data;
    6.     }
    7.     fclose( $f );
    8.     // 上面读取数据
    9.     preg_match_all( "/<humans>(.*?)</humans>/s", $xml, $humans ); //匹配最外层标签里面的内容
    10.     foreach( $humans[1] as $k=>$human )
    11.     {
    12.         preg_match_all( "/<name>(.*?)</name>/", $human, $name ); //匹配出名字
    13.         preg_match_all( "/<sex>(.*?)</sex>/", $human, $sex ); //匹配出性别
    14.         preg_match_all( "/<old>(.*?)</old>/", $human, $old ); //匹配出年龄
    15.     }
    16.     foreach($name[1] as $key=>$val){
    17.         echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."<br>" ;
    18.     }
    19. ?>
    复制代码

    4)xmlreader来读取xml数据

    1. <?php
    2.     $reader = new XMLReader();
    3.     $reader->open('person.xml'); //读取xml数据
    4.     $i=1;
    5.     while ($reader->read()) { //是否读取
    6.         if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
    7.             if($i%3){
    8.                 echo $reader->value; //取得node的值
    9.             }else{
    10.                 echo $reader->value."<br>" ;
    11.             }
    12.             $i++;
    13.         }
    14.     }
    15. ?>
    A buddhist programmer.
  • 相关阅读:
    (转)样本方差的期望
    (转)Python 字典排序
    曝光补偿
    python判断字符串是否包含子字符串
    python requests接口测试 -----博客园串接口
    jmeter+ant+jenkins 搭建接口自动化测试
    TOMCAT闪退。cmd执行startup.bat保错:the CATALINA_HOME environment variable is not defined correctly
    selenium python自动化测试 ddt数据驱动
    jenkins到底如何拉取代码 如何部署的
    git 常用命令
  • 原文地址:https://www.cnblogs.com/wszz/p/8065618.html
Copyright © 2011-2022 走看看