日期:2014-05-20 浏览次数:20857 次
<?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>
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()); } }