日期:2014-05-20  浏览次数:20857 次

关于SAX解析Xml的一点心得体会
本人新手,也就随便写写,有兴趣的可以看下!

SAX解析XML十分简单,可是将解析出来的数据分类存储就不那么容易了!我的XML源文件如下:
XML code
<?xml version="1.0" encoding="UTF-8"?>
<config>
   <Vms total_Vms="5">
      <Vm>
          <Vm_ID>0</Vm_ID>
          <Vm_mips>278</Vm_mips>
      </Vm>
      <Vm>
          <Vm_ID>1</Vm_ID>
          <Vm_mips>289</Vm_mips>
      </Vm>
      <Vm>
          <Vm_ID>2</Vm_ID>
          <Vm_mips>132</Vm_mips>
      </Vm>
      <Vm>
          <Vm_ID>3</Vm_ID>
          <Vm_mips>209</Vm_mips>
      </Vm>
      <Vm>
          <Vm_ID>4</Vm_ID>
          <Vm_mips>286</Vm_mips>
      </Vm>
   </Vms>


   <Cloudlets total_Cloudlets="10">
      <Cloudlet>
          <Cloudlet_ID>0</Cloudlet_ID> 
          <Cloudlet_length>19365</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>1</Cloudlet_ID> 
          <Cloudlet_length>49809</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>2</Cloudlet_ID> 
          <Cloudlet_length>30218</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>3</Cloudlet_ID> 
          <Cloudlet_length>44157</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>4</Cloudlet_ID> 
          <Cloudlet_length>16754</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>5</Cloudlet_ID> 
          <Cloudlet_length>18336</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>6</Cloudlet_ID> 
          <Cloudlet_length>20045</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>7</Cloudlet_ID> 
          <Cloudlet_length>31493</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>8</Cloudlet_ID> 
          <Cloudlet_length>30727</Cloudlet_length> 
      </Cloudlet>
      <Cloudlet>
          <Cloudlet_ID>9</Cloudlet_ID> 
          <Cloudlet_length>31017</Cloudlet_length> 
      </Cloudlet>      
   </Cloudlets>
</config>


我用了4个List(vmId、vmMips、CloudletId、CloudletLength)来保存解析出来的数据。saxParse会读取InputStream来一行一行的解析xml文件中的数据,saxParse解析xml文件的核心方法是parse(InputStream in,DefaultsHandler dh),我们只需要用这个方法就好。DefaultHandler里面的方法全部都是空的,我们需要设计自己的handler通过继承来重写里面一些比较重要的方法,在我的代码里面重写了5个方法,其中后面3个是必须的!

Java code

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParseUtil {
 
     static List<String> vmId=new ArrayList<String>();
     static List<String> vmMips=new ArrayList<String>();
     static List<String> cloudletId=new ArrayList<String>();
     static List<String> cloudletLength=new ArrayList<String>();
     
     private class MyHandle extends DefaultHandler{
         String qqName=null;
         String temp=null;
         
           @Override
        public void startDocument() throws SAXException {
            // TODO Auto-generated method stub
            super.startDocument();
            System.out.println("开始解析!");
        }
          
           @Override
        public void endDocument() throws SAXException {
            // TODO Auto-generated method stub
            super.endDocument();
            System.out.println("解析结束!");
        }
           
           @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            super.startElement(uri, localName, qName, attributes);
            if(qName.equals("Vm_ID")) 
                  qqName=qName;
            if(qName.equals("Vm_mips"))
                  qqName=qName;
            if(qName.equals("Cloudlet_ID"))
                  qqName=qName;
            if(qName.equals("Cloudlet_length"))
                  qqName=qName;
        }
           
           @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            // TODO Auto-generated method stub
             super.endElement(uri, localName, qName);
             if(qName=="Vm_ID"){
                    vmId.add(temp);
                    System.out.println(qName+":"+temp);
             }
             if(qName=="Vm_mips"){
                vmMips.add(temp);
                    System.out.println(qName+":"+temp);
             }
                     
             if(qName=="Cloudlet_ID"){
                    cloudletId.add(temp);
                    System.out.println(qName+":"+temp);
             }
                     
             if(qName=="Cloudlet_length"){
                    cloudletLength.add(temp);
                    System.out.println(qName+":"+temp);
             }
                    
        }
           
           @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            // TODO Auto-generated method stub
            super.characters(ch, start, length);
            //System.out.println(new String(ch,start,length));
            if(qqName=="Vm_ID")
                temp=new String(ch,start,length);
            if(qqName=="Vm_mips")
                temp=new String(ch,start,length);
            if(qqName=="Cloudlet_ID")
                temp=new String(ch,start,length);
            if(qqName=="Cloudlet_length")
                temp=new String(ch,start,length);
        }
     }
     
     public void parseXML(String fileName) {
         try {
             SAXParserFactory saxfac=SAXParserFactory.newInstance();
             SAXParser parse=saxfac.newSAXParser();
             InputStream in=new FileInputStream(fileName);
             parse.parse(in, new MyHandle());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
     }

     public static void main(String[] args) {
        new SAXParseUtil().parseXML("/Users/apple/documents/configuration.xml");
        System.out.println();
        System.out.print("vmId:");
        for(String s:vmId)System.out.print(s+" ");    System.out.println();
        System.out.print("vmMips:");
        for(String s:vmMips)System.out.print(s+" ");    System.out.println();
        System.out.print("cloudletId:");
        for(String s:cloudletId)System.out.print(s+" ");    System.out.println();
        System.out.print("cloudletLength:");
            for(String s:cloudletLength)System.out.print(s+" ");    System.out.println();
        System.out.println();
            System.out.println("The size of vmId is:"+vmId.size()+"\n"+"The size of vmMips is:"+vmMips.size()+"\n"+
                           "The size of CloudletId is:"+cloudletId.size()+"\n"+"The size of " +
                           "CloudletLength is:"+cloudletLength.size());
    }
}