dom解析xml import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class DOMParsePage { public DOMParsePage() { DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); try { DocumentBuilder dombuilder=domfac.newDocumentBuilder(); InputStream is=new FileInputStream("C:/123.xml"); Document doc=dombuilder.parse(is); Element root=doc.getDocumentElement(); NodeList books=root.getChildNodes(); if(books!=null){ for(int i=0;i<books.getLength();i++){ Node book=books.item(i); for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) { if(node.getNodeType()==Node.ELEMENT_NODE){ if(node.getNodeName().equals("title")){ String bookname=node.getFirstChild().getNodeValue(); System.out.println(bookname); } if(node.getNodeName().equals("author")){ String author1=node.getFirstChild().getNodeValue(); System.out.println(author1); } if(node.getNodeName().equals("description")){ String addtime=node.getFirstChild().getNodeValue(); System.out.println(addtime); } if(node.getNodeName().equals("pubDate")){ String price=node.getFirstChild().getNodeValue(); System.out.println(price); } } } } } }catch (ParserConfigurationException e) { e.printStackTrace(); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (SAXException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new DOMParsePage(); } }
SAX解析XML package simpleTest; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.Locator; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.helpers.DefaultHandler; import java.io.IOException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; class TestSAX extends DefaultHandler { private StringBuffer buf; public TestSAX() { super(); } public void setDocumentLocator(Locator locator) { } public void startDocument() throws SAXException { buf=new StringBuffer(); System.out.println("*******开始解析文档*******"); } public void endDocument() throws SAXException { System.out.println("*******文档解析结束*******"); } public void startPrefixMapping( String prefix, String uri ) { System.out.println("\n前缀映射: " + prefix +" 开始!"+ " 它的URI是:" + uri); } public void endPrefixMapping( String prefix ) { System.out.println("\n前缀映射: "+prefix+" 结束!"); } public void processingInstruction( String target, String instruction ) throws SAXException { } public void ignorableWhitespace( char[] chars, int start, int length ) throws SAXException { } public void skippedEntity( String name ) throws SAXException { } public void startElement(String namespaceURI,String localName,String qName,Attributes atts) { System.out.println("*******开始解析元素*******"); System.out.println("元素名"+qName); for(int i=0;i<atts.getLength();i++) { System.out.println("元素名"+atts.getLocalName(i)+"属性值"+atts.getValue(i)); } } public void endElement(String namespaceURI,String localName,String fullName )throws SAXException { System.out.println("******元素解析结束********"); } public void characters( char[] chars, int start, int length )throws SAXException { //将元素内容累加到StringBuffer中 buf.append(chars,start,length); } public static void main(String args[]) { try{ SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); TestSAX testsax=new TestSAX(); sp.parse(new InputSource("D:\\test\\simpleTest\\classes\\simpleTest\\test.xml"),testsax); }catch(IOException e) { e.printStackTrace(); }catch(SAXException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); } } }
XML文件如下: <?xml version="1.0" encoding="gb2312"?> <row> <person> <name>王小明</name> <college>信息学院</college> <telephone>6258113</telephone> <notes>男,1955年生,博士,95年调入海南大学</notes>
原文地址:http://blog.csdn.net/xiaokui_wingfly/article/details/46453363