Java中的两种XML解析技术DOM和SAX
Java中的两种XML解析技术DOM和SAX
目标文件:D:/meno.xml
<memo> <note type="birthday"> <name>王平</name> <sex>男</sex> <date>12月23日</date> <address>吉林省白山市第八中学</address> <telphone>0439-31344532</telphone> <email>wangping@sina.com</email> <body>今天是王平生日</body> </note> <note type="shop"> <name>东方雪</name> <sex>女</sex> <date>8月23日</date> <address>北京海淀区中关村E世界</address> <telphone>15984561235</telphone> <email>eworld@vip.com</email> <body>今天是东方雪店铺开张的日子</body> </note> </memo>
DOM解析
步骤:
1.获取factory
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
2.获取builder
builder = builderFactory.newDocumentBuilder();
3.解析为Document对象
Document document = builder.parse(xmlFile);
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DomTest { public static void main(String[] args) { File xmlFile = new File("D:\\memo.xml"); DocumentBuilder builder = null; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); try { builder = builderFactory.newDocumentBuilder(); Document document = builder.parse(xmlFile); //解析文件 Element root = document.getDocumentElement(); //获得根元素 System.out.println("根元素:" + root.getNodeName()); NodeList childNodes = root.getChildNodes(); //获得根元素下的子节点 for (int i = 0; i < childNodes.getLength(); i++) { //遍历这些子节点 Node node = childNodes.item(i); if("note".equals(node.getNodeName())) { //如果节点的名称为"node",则输出note元素属性type System.out.println("\r\n 找到一篇日记。所属分类:" + node.getAttributes().getNamedItem("type").getNodeValue()+"."); NodeList nodeDetail = node.getChildNodes(); for(int j = 0; j < nodeDetail.getLength(); j++) { Node detail = nodeDetail.item(j); if("name".equals(detail.getNodeName())) System.out.println("姓名: " + detail.getTextContent()); else if("sex".equals(detail.getNodeName())) System.out.println("性别:" + detail.getTextContent()); else if("date".equals(detail.getNodeName())) System.out.println("日期:" + detail.getTextContent()); else if("address:".equals(detail.getNodeName())) System.out.println("居住地址:" + detail.getTextContent()); else if("telephone".equals(detail.getNodeName())) System.out.println("电话号码:" + detail.getTextContent()); else if("email".equals(detail.getNodeName())) System.out.println("E-mail: " + detail.getTextContent()); else if("body".equals(detail.getNodeName())) System.out.println("主题:" + detail.getTextContent()); } } } } catch(Exception e) { e.printStackTrace(); } } }
SAX解析
步骤:
1.获取factory
SAXParserFactory factory = SAXParserFactory.newInstance();
2.获取parser
SAXParser parser = factory.newSAXParser();
3.开始解析
parser.parse(xmlFile,new MySaxListener());
import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class SaxTest { public static void main(String[] args) { File xmlFile = new File("D:\\memo.xml"); SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser parser = factory.newSAXParser(); parser.parse(xmlFile,new MySaxListener()); } catch(Exception e) { e.printStackTrace(); } } }
import org.xml.sax.helpers.DefaultHandler; import java.text.DateFormat; import java.text.SimpleDateFormat; import org.xml.sax.Attributes; import org.xml.sax.SAXException; public class MySaxListener extends DefaultHandler { private String content; public void characters(char[] ch, int start, int length) throws SAXException { content = new String(ch, start, length); } public void endElement(String uri, String localName, String qName) throws SAXException { if("name".equals(qName)) { System.out.println("姓名:" + content); } else if("sex".equals(qName)) { System.out.println("性别:" + content); }else if("date".equals(qName)) { System.out.println("日期:" + content); }else if("address".equals(qName)) { System.out.println("居住地址:" + content); }else if("telephone".equals(qName)) { System.out.println("电话号码:" + content); }else if("email".equals(qName)) { System.out.println("电子邮件:" + content); }else if("body".equals(qName)) { System.out.println("主题:" + content); } } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("note".equals(qName)) { System.out.println("下面是一个关于SAX的解析方式:"); System.out.println("\r\n找到一篇文章.所属分类:" + attributes.getValue("type") + "."); } } }
解析结果