Java使用pull解析xml文件

使用pull解析xml需要依赖:kxml2-2.3.0.jar和xmlpull_1_1_3_4c.jar

xml文件结构如下:

  1.0" encoding="UTF-8"?>

reader” cardType=“ Data“>

<DependedCard>
    <Card name="<span style="color: #8b0000">Handler</span>" slotNo="<span style="color: #8b0000">2</span>" rate="<span style="color: #8b0000">9600</span>"></Card>
</DependedCard>
<CardInfo name="<span style="color: #8b0000">信息</span>" slotNo="<span style="color: #8b0000">1</span>" rate="<span style="color: #8b0000">9600</span>">
    <Context <span style="color: #0000ff">class</span>="<span style="color: #8b0000">MF</span>" id="<span style="color: #8b0000">3F00</span>">
        <Section <span style="color: #0000ff">class</span>="<span style="color: #8b0000">KEY</span>" id="<span style="color: #8b0000">0000</span>" size="<span style="color: #8b0000"></span>"  index="<span style="color: #8b0000"></span>">
        </Section>
        <Section <span style="color: #0000ff">class</span>="<span style="color: #8b0000">INFO_FILE</span>" id="<span style="color: #8b0000">0015</span>" size="<span style="color: #8b0000">08</span>"  index="<span style="color: #8b0000"></span>">
        </Section>
        <Section <span style="color: #0000ff">class</span>="<span style="color: #8b0000">DFDF</span>" id="<span style="color: #8b0000">1001</span>"  size="<span style="color: #8b0000"></span>" index="<span style="color: #8b0000">0</span>">
            <Element description="<span style="color: #8b0000">文件</span>" <span style="color: #0000ff">class</span>="<span style="color: #8b0000">KEY</span>" id="<span style="color: #8b0000">0000</span>" parentADFindex="<span style="color: #8b0000">0</span>">
                <Cell description="<span style="color: #8b0000">数据</span>"                 type="<span style="color: #8b0000">XX</span>" count="<span style="color: #8b0000"></span>" step="<span style="color: #8b0000"></span>" fd="<span style="color: #8b0000"></span>" matherKeyId="<span style="color: #8b0000"></span>" algorithmTag="<span style="color: #8b0000"></span>" useChange="<span style="color: #8b0000"></span>" state="<span style="color: #8b0000"></span>" countState="<span style="color: #8b0000"></span>"></Cell>
                <Cell description="<span style="color: #8b0000">数据</span>"               type="<span style="color: #8b0000">YY</span>" count="<span style="color: #8b0000">5</span>" step="<span style="color: #8b0000">1</span>" fd="<span style="color: #8b0000">0</span>" matherKeyId="<span style="color: #8b0000">06</span>" algorithmTag="<span style="color: #8b0000">00</span>" useChange="<span style="color: #8b0000">1A</span>" state="<span style="color: #8b0000"></span>" countState="<span style="color: #8b0000"></span>"></Cell>   
            </Element>
            <Element description="<span style="color: #8b0000">数据文件</span>"     <span style="color: #0000ff">class</span>="<span style="color: #8b0000">RECORD_FILE</span>" id="<span style="color: #8b0000">0002</span>" parentADFindex="<span style="color: #8b0000">0</span>">
            </Element>
            <Element description="<span style="color: #8b0000">审计文件</span>"         <span style="color: #0000ff">class</span>="<span style="color: #8b0000">RECORD_FILE</span>" id="<span style="color: #8b0000">0003</span>" parentADFindex="<span style="color: #8b0000">0</span>">
            </Element>
            <Element description="<span style="color: #8b0000">计数文件</span>"     <span style="color: #0000ff">class</span>="<span style="color: #8b0000">RECORD_FILE</span>" id="<span style="color: #8b0000">0004</span>" parentADFindex="<span style="color: #8b0000">0</span>">
            </Element>
            <Element description="<span style="color: #8b0000">记录文件</span>" <span style="color: #0000ff">class</span>="<span style="color: #8b0000">RECORD_FILE</span>" id="<span style="color: #8b0000">0005</span>" parentADFindex="<span style="color: #8b0000">0</span>">
            </Element>
        </Section>
    </Context>
</CardInfo>


解析的java程序(里面的实体类在程序之后):

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class XmlPullHandler {
    private Cell cell;
    private Element element;
    private Section section;
    private Card card;
    private DependedCard dependedCard;
    private Context context;
    private CardInfo cardInfo;
    private CheckCards checkCards;
    private List

  

  
   
 
   
  cardInfoList;

    
 
   
 
   private List
 
   
 
   
     cellList; 
    private List 
    
      elementList; 
     private List 
     
       sectionList; 
      private List 
      
        cardList; 
       private InputStream inputStream; 
       public XmlPullHandler(InputStream inputStream){ 
       this.inputStream = inputStream; } 
       public CheckCards parseXml(){ 
       //构建XmlPullParserFactory 
       try { XmlPullParserFactory pullParserFactory=XmlPullParserFactory.newInstance(); 
       //获取XmlPullParser的实例 XmlPullParser xmlPullParser=pullParserFactory.newPullParser(); 
       //设置输入流 xml文件 xmlPullParser.setInput(inputStream, " 
       UTF-8"); 
       //开始 
       int eventType=xmlPullParser.getEventType(); 
       try { 
       while(eventType!=XmlPullParser.END_DOCUMENT){ String qName=xmlPullParser.getName(); 
       switch (eventType) { 
       //文档开始 
       case XmlPullParser.START_DOCUMENT: 
       //cardInfo = new CardInfo(); 
       break; 
       //开始节点 
       case XmlPullParser.START_TAG: 
       if(" 
       CheckCards".equals(qName)){ checkCards = 
       new CheckCards(); cardInfoList = 
       new ArrayList 
       
         (); checkCards.setDeviceType(xmlPullParser.getAttributeValue( 
        null," 
        deviceType")); checkCards.setCardType(xmlPullParser.getAttributeValue( 
        null," 
        cardType")); } 
        else 
        if (" 
        DependedCard".equals(qName)) { dependedCard = 
        new DependedCard(); cardList = 
        new ArrayList 
        
          (); } 
         else 
         if (" 
         Card".equals(qName)) { card = 
         new Card(); card.setName(xmlPullParser.getAttributeValue( 
         null," 
         name")); card.setSlotNo(xmlPullParser.getAttributeValue( 
         null," 
         slotNo")); card.setRate(xmlPullParser.getAttributeValue( 
         null," 
         rate")); } 
         else 
         if(" 
         CardInfo".equals(qName)){ cardInfo = 
         new CardInfo(); cardInfo.setName(xmlPullParser.getAttributeValue( 
         null," 
         name")); cardInfo.setSlotNo(xmlPullParser.getAttributeValue( 
         null," 
         slotNo")); cardInfo.setRate(xmlPullParser.getAttributeValue( 
         null," 
         rate")); } 
         else 
         if (" 
         Context".equals(qName)) { context = 
         new Context(); sectionList = 
         new ArrayList 
         
           (); context.setContextClass(xmlPullParser.getAttributeValue( 
          null," 
          class")); context.setContextId(xmlPullParser.getAttributeValue( 
          null," 
          id")); } 
          else 
          if (" 
          Section".equals(qName)) { section = 
          new Section(); elementList = 
          new ArrayList 
          
            (); section.setSectionCalssName(xmlPullParser.getAttributeValue( 
           null," 
           class")); section.setSectionId(xmlPullParser.getAttributeValue( 
           null," 
           id")); section.setSize(xmlPullParser.getAttributeValue( 
           null," 
           size")); section.setIndex(xmlPullParser.getAttributeValue( 
           null," 
           index")); } 
           else 
           if (" 
           Element".equals(qName)) { element = 
           new Element(); cellList = 
           new ArrayList 
           
             (); element.setDescription(xmlPullParser.getAttributeValue( 
            null," 
            description")); element.setElementCalssName(xmlPullParser.getAttributeValue( 
            null," 
            class")); element.setElementId(xmlPullParser.getAttributeValue( 
            null," 
            id")); element.setParentADFindex(xmlPullParser.getAttributeValue( 
            null," 
            parentADFindex")); } 
            else 
            if (" 
            Cell".equals(qName)) { cell = 
            new Cell(); cell.setDescription(xmlPullParser.getAttributeValue( 
            null," 
            description")); cell.setType(xmlPullParser.getAttributeValue( 
            null," 
            type")); cell.setCount(xmlPullParser.getAttributeValue( 
            null," 
            count")); cell.setStep(xmlPullParser.getAttributeValue( 
            null," 
            step")); cell.setFd(xmlPullParser.getAttributeValue( 
            null," 
            fd")); cell.setMatherKeyId(xmlPullParser.getAttributeValue( 
            null," 
            matherKeyId")); cell.setAlgorithmTag(xmlPullParser.getAttributeValue( 
            null," 
            algorithmTag")); cell.setUseChange(xmlPullParser.getAttributeValue( 
            null," 
            useChange")); cell.setState(xmlPullParser.getAttributeValue( 
            null," 
            state")); cell.setCountState(xmlPullParser.getAttributeValue( 
            null," 
            countState")); cell.setKeyValue(xmlPullParser.getAttributeValue( 
            null," 
            keyValue")); } 
            break; 
            //结束节点 
            case XmlPullParser.END_TAG: 
            if(" 
            Card".equals(qName)){ cardList.add(card); card = 
            null; } 
            else 
            if(" 
            DependedCard".equals(qName)){ dependedCard.setCardList(cardList); cardList = 
            null; } 
            else 
            if(" 
            Cell".equals(qName)){ cellList.add(cell); cell = 
            null; } 
            else 
            if(" 
            Element".equals(qName)){ element.setCellList(cellList); elementList.add(element); element = 
            null; cellList = 
            null; } 
            else 
            if(" 
            Section".equals(qName)){ section.setElementList(elementList); sectionList.add(section); section = 
            null; elementList = 
            null; } 
            else 
            if(" 
            Context".equals(qName)){ context.setSectionList(sectionList); sectionList = 
            null; } 
            else 
            if(" 
            CardInfo".equals(qName)){ cardInfo.setContext(context); cardInfoList.add(cardInfo); context = 
            null; cardInfo = 
            null; } 
            else 
            if(" 
            CheckCards".equals(qName)){ checkCards.setDependedCard(dependedCard); checkCards.setCardInfoList(cardInfoList); dependedCard = 
            null; cardInfoList = 
            null; } 
            break; 
            default: 
            break; } eventType=xmlPullParser.next(); } } 
            catch (NumberFormatException e) { e.printStackTrace(); } 
            catch (IOException e) { e.printStackTrace(); } } 
            catch (XmlPullParserException e) { e.printStackTrace(); } 
            return checkCards; } } 
            
           
          
         
        
       
      
     
   

  

  

程序中的实体类如下:

Card类

public class Card {

    private String name;
    private String slotNo;
    private String rate;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSlotNo() {
        return slotNo;
    }
    public void setSlotNo(String slotNo) {
        this.slotNo = slotNo;
    }
    public String getRate() {
        return rate;
    }
    public void setRate(String rate) {
        this.rate = rate;
    }

}

CardInfo类

public class CardInfo {

    private String name;
    private String slotNo;
    private Context context;
    private String rate;
    public String getSlotNo() {
        return slotNo;
    }
    public void setSlotNo(String slotNo) {
        this.slotNo = slotNo;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Context getContext() {
        return context;
    }
    public void setContext(Context context) {
        this.context = context;
    }
    public String getRate() {
        return rate;
    }
    public void setRate(String rate) {
        this.rate = rate;
    }

}

Cell类

public class Cell {

    private String description;
    private String type;
    private String count;
    private String step;
    private String fd;
    private String matherKeyId;
    private String algorithmTag;
    private String useChange;
    private String state;
    private String countState;
    private String keyValue;
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
    public String getStep() {
        return step;
    }
    public void setStep(String step) {
        this.step = step;
    }
    public String getFd() {
        return fd;
    }
    public void setFd(String fd) {
        this.fd = fd;
    }
    public String getMatherKeyId() {
        return matherKeyId;
    }
    public void setMatherKeyId(String matherKeyId) {
        this.matherKeyId = matherKeyId;
    }
    public String getAlgorithmTag() {
        return algorithmTag;
    }
    public void setAlgorithmTag(String algorithmTag) {
        this.algorithmTag = algorithmTag;
    }
    public String getUseChange() {
        return useChange;
    }
    public void setUseChange(String useChange) {
        this.useChange = useChange;
    }

    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getCountState() {
        return countState;
    }
    public void setCountState(String countState) {
        this.countState = countState;
    }
    public String getKeyValue() {
        return keyValue;
    }
    public void setKeyValue(String keyValue) {
        this.keyValue = keyValue;
    }
}

CheckCards类

public class CheckCards {

    private String deviceType;
    private String cardType;
    private DependedCard dependedCard;
    private List

  

  
   
 
   
  cardInfoList;
    
 
   
 
   public String getDeviceType() {
        
 
   
 
   return deviceType;
    }
    
 
   
 
   public 
 
   
 
   void setDeviceType(String deviceType) {
        
 
   
 
   this.deviceType = deviceType;
    }
    
 
   
 
   public String getCardType() {
        
 
   
 
   return cardType;
    }
    
 
   
 
   public 
 
   
 
   void setCardType(String cardType) {
        
 
   
 
   this.cardType = cardType;
    }
    
 
   
 
   public DependedCard getDependedCard() {
        
 
   
 
   return dependedCard;
    }
    
 
   
 
   public 
 
   
 
   void setDependedCard(DependedCard dependedCard) {
        
 
   
 
   this.dependedCard = dependedCard;
    }
    
 
   
 
   public List
 
   
 
   
     getCardInfoList() { 
    return cardInfoList; } 
    public 
    void setCardInfoList(List 
    
      cardInfoList) { 
     this.cardInfoList = cardInfoList; } } 
     
   

  

  

Context类

public class Context {

    private String contextClass;
    private String contextId;
    private List

  

  
   
 
   
  sectionList;
    
 
   
 
   public String getContextClass() {
        
 
   
 
   return contextClass;
    }
    
 
   
 
   public 
 
   
 
   void setContextClass(String contextClass) {
        
 
   
 
   this.contextClass = contextClass;
    }
    
 
   
 
   public String getContextId() {
        
 
   
 
   return contextId;
    }
    
 
   
 
   public 
 
   
 
   void setContextId(String contextId) {
        
 
   
 
   this.contextId = contextId;
    }
    
 
   
 
   public List
 
   
 
   
     getSectionList() { 
    return sectionList; } 
    public 
    void setSectionList(List 
    
      sectionList) { 
     this.sectionList = sectionList; } } 
     
   

  

  

DependedCard类

public class DependedCard {

    private List

  

  
   
 
   
  cardList;

    
 
   
 
   public List
 
   
 
   
     getCardList() { 
    return cardList; } 
    public 
    void setCardList(List 
    
      cardList) { 
     this.cardList = cardList; } } 
     
   

  

  

Element类

public class Element {

    private String description;
    private String elementCalssName;
    private String elementId;
    private String parentADFindex;
    private List

  

  
   
 
   
  cellList;

    
 
   
 
   public String getDescription() {
        
 
   
 
   return description;
    }
    
 
   
 
   public 
 
   
 
   void setDescription(String description) {
        
 
   
 
   this.description = description;
    }
    
 
   
 
   public String getElementCalssName() {
        
 
   
 
   return elementCalssName;
    }
    
 
   
 
   public 
 
   
 
   void setElementCalssName(String elementCalssName) {
        
 
   
 
   this.elementCalssName = elementCalssName;
    }
    
 
   
 
   public String getElementId() {
        
 
   
 
   return elementId;
    }
    
 
   
 
   public 
 
   
 
   void setElementId(String elementId) {
        
 
   
 
   this.elementId = elementId;
    }

    
 
   
 
   public String getParentADFindex() {
        
 
   
 
   return parentADFindex;
    }
    
 
   
 
   public 
 
   
 
   void setParentADFindex(String parentADFindex) {
        
 
   
 
   this.parentADFindex = parentADFindex;
    }
    
 
   
 
   public List
 
   
 
   
     getCellList() { 
    return cellList; } 
    public 
    void setCellList(List 
    
      cellList) { 
     this.cellList = cellList; } } 
     
   

  

  

Section类

public class Section {
    private String sectionCalssName;
    private String sectionId;
    private String size;
    private String index;
    private List

  

  
   
 
   
  elementList;

    
 
   
 
   public String getSectionCalssName() {
        
 
   
 
   return sectionCalssName;
    }
    
 
   
 
   public 
 
   
 
   void setSectionCalssName(String sectionCalssName) {
        
 
   
 
   this.sectionCalssName = sectionCalssName;
    }
    
 
   
 
   public String getSectionId() {
        
 
   
 
   return sectionId;
    }
    
 
   
 
   public 
 
   
 
   void setSectionId(String sectionId) {
        
 
   
 
   this.sectionId = sectionId;
    }
    
 
   
 
   public List
 
   
 
   
     getElementList() { 
    return elementList; } 
    public 
    void setElementList(List 
    
      elementList) { 
     this.elementList = elementList; } 
     public String getSize() { 
     return size; } 
     public 
     void setSize(String size) { 
     this.size = size; } 
     public String getIndex() { 
     return index; } 
     public 
     void setIndex(String index) { 
     this.index = index; } } 
     
   

  

  

你可能感兴趣的:(Java使用pull解析xml文件)