xml的sax解析

public void parseXml04(){  
	       String xmlPath = "C:\\Users\\asus\\Desktop\\map.osm.xml";    
	    try {  
	        //获取SAX分析器的工厂实例,专门负责创建SAXParser分析器  
	        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();  
	        //获取SAXParser分析器的实例  
	        SAXParser saxParser = saxParserFactory.newSAXParser();  
	           InputStream inputStream = new FileInputStream(new File(xmlPath));    
	          
	        //解析xml文档  
	        saxParser.parse(inputStream, new XmlSAXHandler04());  
	          
	        //迭代list  
	        System.out.println(nodes.size()+","+ways.size());
	        if (XmlRead.ways.size()>0) {
				for (Way way:XmlRead.ways) {
					System.out.println("-----------------------------------------");
					System.out.println("【Id】"+way.getId());
					for (String string:way.getList()) {
						System.out.println("【Nd】"+string);
					}
				}
			}
	    } catch (ParserConfigurationException e) {  
	        e.printStackTrace();  
	    } catch (SAXException e) {  
	        e.printStackTrace();  
	    } catch (FileNotFoundException e) {  
	        e.printStackTrace();  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    }  
	}  
class XmlSAXHandler04 extends DefaultHandler {    
    
      
    @Override  
    public void startDocument() throws SAXException {  
        XmlRead.nodes=new ArrayList();
        XmlRead.ways=new ArrayList();
        XmlRead.list=new ArrayList();
    }  
      
    @Override  
    public void endDocument() throws SAXException {  
          
    }  
      
    @Override  
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {  
 
         
        if (qName.equals("node")) {
			XmlRead.node=new Node();
			if (attributes.getLength()>0) {
				XmlRead.node.setIdString(attributes.getValue("id").toString());
				XmlRead.node.setLat(attributes.getValue("lat").toString());
				XmlRead.node.setLon(attributes.getValue("lon").toString());
			}
		}
        if (qName.equals("way")) {
			XmlRead.way=new Way();
			if (attributes.getLength()>0) {
				XmlRead.way.setId(attributes.getValue("id").toString());
			}
		}
        if (qName.equals("nd")) {
			XmlRead.list=new ArrayList();
			if (attributes.getLength()>0) {
				XmlRead.string=attributes.getValue("ref").toString();
//				System.out.println(XmlRead.list.size());
			}
		}
    }  
  
    @Override  
    public void endElement(String uri, String localName, String qName) throws SAXException {  
        //需要说明的是,因为每一个非空标签都有characters(),那么无法知道user子标签循环完了  
        //但是可以这样,如果不考虑子标签顺序可以判断是否解析到了最后一个子标签来判断  
        //或者直接在user标签的endElement()中添加即可。  
 
        
        
        
        if (qName.equals("node")) {
			XmlRead.nodes.add(XmlRead.node);
			XmlRead.node=null;
		}
        if (qName.equals("way")) {
        	XmlRead.way.setList(XmlRead.list);
			XmlRead.ways.add(XmlRead.way);
			System.out.println(XmlRead.list.size());
			XmlRead.way=null;
			XmlRead.list=null;
		}
        if (qName.equals("nd")) {
			XmlRead.list.add(XmlRead.string);
			XmlRead.string=null;
		}
    }  
      
    @Override  
    public void characters(char[] ch, int start, int length) throws SAXException {  

    }  
}  


你可能感兴趣的:(xml)