首页 > 代码库 > stax解析XML-----Demo

stax解析XML-----Demo

 

Demo

 1 public void staxDemo() throws Exception 2     { 3         XMLStreamReader xml= XMLInputFactory.newInstance().createXMLStreamReader(Demo.class.getClassLoader().getResourceAsStream("users.xml")); 4         int tag=xml.next(); 5         while(tag!=XMLStreamReader.END_DOCUMENT) 6         { 7             if(tag==XMLStreamReader.START_ELEMENT) 8             { 9                 if(xml.getLocalName().equals("user"))10                 {11                     String id=xml.getAttributeValue(0);12                     System.err.println("id》》"+id);13                 }14                 if(xml.getLocalName().equals("name"))15                 {16                     String name=xml.getElementText();17                     System.err.println("name》"+name);18                 }19                 if(xml.getLocalName().equals("age"))20                 {21                     String age=xml.getElementText();22                     System.err.println("age>>"+age);23                 }24                 25             }26             tag=xml.next();27             28         }29     }

 

stax解析XML-----Demo