首页 > 代码库 > pull解析

pull解析

需要导包:xmlpull_1_1_3_1.jar 和kxml2-2.3.0.jar

package tan. pull;

import java. io.FileReader ;
import org. xmlpull.v1 .XmlPullParser;
import org. xmlpull.v1 .XmlPullParserFactory;

public class TestPullExe {
      public static void main(String [] args) throws Exception {
           //查询id=007学生
           //1.获取解析器工厂
          XmlPullParserFactory factory = XmlPullParserFactory.newInstance() ;
           //2.使用工厂对象生成解析器对象
          XmlPullParser parser = factory.newPullParser ();
           //3.使用解析器对象加载 xml
          parser .setInput(new FileReader("student.xml" ));
          
           //读取到指定内容的标示符
           boolean isReadId =false ;
           boolean isReadName =false ;
          
           while(parser .next()!=XmlPullParser .END_DOCUMENT){
                if(parser .getEventType() ==XmlPullParser. START_TAG){
                    //读到id且等于007这设置isReadId=true;
                   String idStr = parser.getAttributeValue( null, "id") ;
                    if("007" .equals(idStr)){
                        isReadId =true ;
                    }
                    //读到name,且isReadName=true;
                    if("name" .equals(parser. getName()) && isReadId){
                        isReadName =true ;
                    }
               }
                if(parser .getEventType() ==XmlPullParser. TEXT){
               //如果
               if(isReadId && isReadName){
                   System .out. println( "name:"+parser .getText());
                    break;
               }
                   
               }
           }
          
          System .out. println( parser.getEventType ()); //4 文本
     
      }
}

pull解析