Java XML :DOM, SAX, JDOM,JAXB
来源:互联网 发布:数据魔方的作用 编辑:程序博客网 时间:2024/05/01 22:56
http://www.mkyong.com/tutorials/java-xml-tutorials/
In Java JDK, two built-in XML parsers are available – DOM andSAX, both have their pros and cons.
In addition, updated JAXB example to show you how to convert object to / from XML.
1.DOM XML Parser
The DOM is the easiest to use Java XML Parser.
DOM Parser is slow and consume a lot memory if it load a XML document which contains a lot of data.
DOM parser parses the entire XML document and loads it into memory; then models it in a “TREE” structure for easy traversal or manipulation.
In short, it turns a XML file into DOM or Tree structure, and you have to traverse a node by node to get what you want.
In the DOM, everything in an XML document is a node, read this.
File fXmlFile = new File("/Users/xml/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
// optional, but recommended
// read this -
// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
2.SAX XML Parser
SAX parser is work differently with DOM parser,
it does not load any XML document into memory and create some object representation of the XML document.
Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler
) to informs clients of the XML document structure.
SAX Parser is faster and uses less memory than DOM parser.
See following SAX callback methods :
- startDocument() and endDocument() – Method called at the start and end of an XML document.
- startElement() and endElement() – Method called at the start and end of a document element.
- characters() – Method called with the text contents in between the start and end tags of an XML document element.
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {}
public void endElement(String uri, String localName,String qName) throws SAXException {
}
public void characters(char ch[], int start, int length) throws SAXException {
}
saxParser.parse("c:\\file.xml", handler);
JDOM is, quite simply, a Java representation of an XML document.
JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing.
It has a straightforward API, is a lightweight and fast, and is optimized for the Java programmer.
It’s an alternative to DOM and SAX, although it integrates well with both DOM and SAX.
JDOM, Java XML parser, is more user friendly in the way of accessing the XML document.
In this example, we show you how to use JDOM to read a XML file, and print out each element orderly.
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("/Users/xml/staff.xml");
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List list = rootNode.getChildren("staff");
5.JAXB, stands for Java Architecture for XML Binding,
using JAXB annotation to convert Java object to / from XML file.
In this tutorial, we show you how to use JAXB to do following stuffs :
- Marshalling – Convert a Java object into a XML file.
- Unmarshalling – Convert XML content into a Java Object.
Technologies used in this article
- JDK 1.6
- JAXB 2.0
Working with JAXB is easy, just annotate object with JAXB annotation, later usejaxbMarshaller.marshal()
or jaxbMarshaller.unmarshal()
to do the object / XML conversion.
- Java XML :DOM, SAX, JDOM,JAXB
- 操作xml文件:DOM、SAX、JDOM、JAXB、Dom4J
- DOM、SAX、JDOM、JAXB 对XML 文档的解析
- java环境下读取xml文件的方法主要有4种:DOM、SAX、JDOM、JAXB
- JAVA解析xml DOM SAX JDOM DOM4J
- 在java环境下读取xml文件的方法主要有4种:DOM、SAX、JDOM、JAXB、Dom4J
- XML(DOM SAX JDOM DOM4J)
- 对XML文件的解析,DOM、SAX、JDOM、JAXB还有dom4
- Java下XML接口比较:DOM SAX JDOM JAXP
- Java下XML编程接口比较:DOM SAX JDOM JAXP
- Java四种方式操作 xml,DOM|SAX|JDOM|DOM4J
- Java中四种XML解析技术对比(Dom、Sax、Dom4j JDom)
- Java四种方式操作 xml,DOM|SAX|JDOM|DOM4J
- java分别采用dom,sax,jdom,dom4j操作xml
- Java解析XML汇总(DOM/SAX/JDOM/DOM4j/XPath)
- Java解析XML汇总(DOM/SAX/JDOM/DOM4j/XPath)
- Java解析XML汇总(DOM/SAX/JDOM/DOM4j/XPath)
- Java解析XML汇总(DOM/SAX/JDOM/DOM4j/XPath)
- 工作流审批平台-辅助功能-角色代理
- sysfs文件系统学习
- ios学习笔记之block在ios开发中的应用
- (Android) NDK
- 在Oracle中实现数据库的复制
- Java XML :DOM, SAX, JDOM,JAXB
- php:PHP分页原理
- sysfs接口函数的建立_DEVICE_ATTR
- 基于mjpg-streamer远程视频WEB监控 项目笔记一
- 64位Linux下安装Matlab常见问题的解决方案
- boa/cgi/html
- UpdatePanel控件(五)——UpdateProgress控件
- FusionCharts图表中组合图的实现
- 嵌入式Linux编辑器vi的使用