浏览 1914 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-07
而sax方式是以缓存的方式流读入的方式,检索输入的文本信息。用方式通知实现方法显示数据的。 所以当文件较大时候,就会出现性能和内存溢出(outofMemoryError)了。 以上是本人的自己的理解,仅供参考,具体的信息还请参考官方文档。 <?xml version="1.0" encoding="utf-8"?> <schoolList> <school id="10001" name="一中" >1000人</school> <school id="10002" name="二中" >2000人</school> <school id="10003" name="三中" >3000人</school> <school id="10004" name="四中" >4000人</school> </schoolList> package com.liuxt.xml.tutorial; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DomParse { public static String getTypeName(short value){ switch(value){ case Node.ELEMENT_NODE: return "ELEMENT_NODE"; case Node.ATTRIBUTE_NODE: return "ATTRIBUTE_NODE"; case Node.CDATA_SECTION_NODE: return "CDATA_SECTION_NODE"; case Node.COMMENT_NODE: return "COMMENT_NODE"; case Node.DOCUMENT_FRAGMENT_NODE: return "DOCUMENT_FRAGMENT_NODE"; case Node.DOCUMENT_NODE: return "DOCUMENT_NODE"; case Node.ENTITY_NODE: return "ENTITY_NODE"; case Node.TEXT_NODE: return "TEXT_NODE"; } return null; } public static void main(String arge[]) { long beginTime = System.currentTimeMillis(); DocumentBuilderFactory factory; DocumentBuilder builder; try { File f = new File("xml/school.xml"); factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList schoolList = doc.getElementsByTagName("school"); Node schoolNode,idNode,nameNode; for (int i = 0; i < schoolList.getLength(); i++) { schoolNode=schoolList.item(i); showNodeInfo(schoolNode); idNode=schoolNode.getAttributes().getNamedItem("id"); showNodeInfo(idNode); nameNode=schoolNode.getAttributes().getNamedItem("id"); showNodeInfo(nameNode); } } catch (Exception e) { e.printStackTrace(); } showTime(beginTime); } private static void showTime(long lasting) { System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); } private static void showNodeInfo(Node schoolNode) { String nodeName; String nodeValue; short nodeType; nodeType=schoolNode.getNodeType(); nodeName=schoolNode.getNodeName(); nodeValue=schoolNode.getNodeValue(); System.out.println("node Type is :"+getTypeName(nodeType)); System.out.println("node name is :"+nodeName); System.out.println("node value is :"+nodeValue); } } package com.liuxt.xml.tutorial; import java.util.Iterator; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class JdomParse { public static void main(String[] args) { try { Document document = new SAXBuilder().build("xml/school.xml"); Element schools = document.getRootElement(); Iterator it = schools.getChildren().iterator(); while (it.hasNext()) { Element school = (Element) it.next(); String id=school.getAttributeValue("id"); String name=school.getAttributeValue("name"); System.out.println("id:"+id +" name:"+name); } } catch (Exception e) { e.printStackTrace(); } } } package com.liuxt.xml.tutorial; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxParse extends DefaultHandler { java.util.Stack tags = new java.util.Stack(); public SaxParse() { super(); } public static void main(String args[]) { long lasting = System.currentTimeMillis(); try { SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); DefaultHandler reader = new SaxParse(); sp.parse(new InputSource("xml/school.xml"), reader); } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); } public void characters(char ch[], int start, int length) throws SAXException { String tag = (String) tags.peek(); if (tag.equals("school")) { System.out.println("学校人数:" + new String(ch, start, length)); } } @SuppressWarnings("unchecked") public void startElement(String uri, String localName, String qName, Attributes attrs) { tags.push(qName); if(qName.equals("school")){ System.out.println(qName); System.out.println("id:"+attrs.getValue("id")); System.out.println("name:"+attrs.getValue("name")); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |