Java解析XMl文件之SAX和DOm方法
如题,这两种方法的jar包都在jdk中,不需要下载。
先来说下目录结构:
首先建一个Peron类封装person.xml的属性;DomParseService和SaxParseService分别为两种解析方法的服务类。最后TestXml为测试类。
Person.xml如下:
1 <?xml version="1.0" encoding="gbk"?> 2 <Persons> 3 <Person id="1"> 4 <name>zhangsan</name> 5 <sex>男</sex> 6 <age>30</age> 7 </Person> 8 <Person id="2"> 9 <name>lisi</name> 10 <sex>女</sex> 11 <age>20</age> 12 </Person> 13 </Persons>
- SAX方法:
1 public class SaxParseService extends DefaultHandler { 2 private List<Person> mPersons = null; 3 private Person mPerson = null; 4 private String m_preTag = null; 5 6 public List<Person> getPersons(InputStream xmlStream) 7 throws ParserConfigurationException, SAXException, IOException { 8 SAXParserFactory factory = SAXParserFactory.newInstance(); 9 SAXParser parser = factory.newSAXParser(); 10 SaxParseService handler = new SaxParseService(); 11 parser.parse(xmlStream, handler); 12 return handler.getPersons(); 13 } 14 15 private List<Person> getPersons() { 16 return mPersons; 17 } 18 19 @Override 20 public void startDocument() throws SAXException { 21 mPersons = new ArrayList<Person>(); 22 } 23 24 @Override 25 public void startElement(String uri, String localName, String qName, 26 Attributes attributes) throws SAXException { 27 if ("Person".equals(qName)) { 28 mPerson = new Person(); 29 mPerson.setId(Integer.parseInt(attributes.getValue(0))); 30 } 31 m_preTag = qName; 32 } 33 34 @Override 35 public void endElement(String uri, String localName, String qName) 36 throws SAXException { 37 if ("Person".equals(qName)) { 38 mPersons.add(mPerson); 39 mPerson = null; 40 } 41 m_preTag = null; 42 } 43 44 @Override 45 public void characters(char[] ch, int start, int length) 46 throws SAXException { 47 if (m_preTag != null) { 48 String content = new String(ch, start, length); 49 if ("name".equals(m_preTag)) { 50 mPerson.setName(content); 51 } else if ("sex".equals(m_preTag)) { 52 mPerson.setSex(content); 53 } else if ("age".equals(m_preTag)) { 54 mPerson.setAge(Integer.parseInt(content)); 55 } 56 } 57 } 58 59 }
- DOM方法:
1 public class DomParseService { 2 public List<Person> getPersons(InputStream is) 3 throws ParserConfigurationException, SAXException, IOException { 4 List<Person> mPersons = new ArrayList<Person>(); 5 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 6 DocumentBuilder builder = factory.newDocumentBuilder(); 7 Document element = builder.parse(is); 8 NodeList PersonNodes = element.getElementsByTagName("Person"); 9 for (int i = 0; i < PersonNodes.getLength(); i++) { 10 Element PersonElement = (Element) PersonNodes.item(i); 11 Person mPerson = new Person(); 12 mPerson.setId(Integer.parseInt(PersonElement.getAttribute("id"))); 13 NodeList ChildNodes = PersonElement.getChildNodes(); 14 for (int j = 0; j < ChildNodes.getLength(); j++) { 15 if (ChildNodes.item(j).getNodeType() == Node.ELEMENT_NODE) { 16 if("name".equals(ChildNodes.item(j).getNodeName())){ 17 mPerson.setName(ChildNodes.item(j).getFirstChild().getNodeValue()); 18 }else if("sex".equals(ChildNodes.item(j).getNodeName())){ 19 mPerson.setSex(ChildNodes.item(j).getFirstChild().getNodeValue()); 20 }else if("age".equals(ChildNodes.item(j).getNodeName())){ 21 mPerson.setAge(Integer.parseInt(ChildNodes.item(j).getFirstChild().getNodeValue())); 22 } 23 } 24 } 25 mPersons.add(mPerson); 26 } 27 return mPersons; 28 29 } 30 }
测试类如下。里面正对SAX和DOM分别写了两个方法:
1 public class TestXml { 2 3 public static void main(String[] args) { 4 SaxParse(); 5 DomParse(); 6 } 7 public static void DomParse() { 8 DomParseService dps=new DomParseService(); 9 try { 10 List<Person> mPersons=dps.getPersons(new FileInputStream(new File("e:/Person.xml"))); 11 for(Person p:mPersons){ 12 System.out.println(p.getName()); 13 } 14 } catch (FileNotFoundException e) { 15 e.printStackTrace(); 16 } catch (ParserConfigurationException e) { 17 e.printStackTrace(); 18 } catch (SAXException e) { 19 e.printStackTrace(); 20 } catch (IOException e) { 21 e.printStackTrace(); 22 } 23 } 24 public static void SaxParse() { 25 SaxParseService sps=new SaxParseService(); 26 try { 27 List<Person> Persons=sps.getPersons(new FileInputStream(new File("e:/Person.xml"))); 28 for(Person p:Persons){ 29 System.out.println(p.getName()); 30 } 31 } catch (FileNotFoundException e) { 32 e.printStackTrace(); 33 } catch (ParserConfigurationException e) { 34 e.printStackTrace(); 35 } catch (SAXException e) { 36 e.printStackTrace(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 42 }
如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨小小打赏一下吧,如果囊中羞涩,不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.