java-各种xml解析技术总结
需要导入相关jar包:
1、dom:
DOMParser dOMParser = new DOMParser();
dOMParser.parse(fileName);
Document document = dOMParser.getDocument();
Element element = document.getDocumentElement();
NodeList studentNl = element.getElementsByTagName("student");
2、jdom:
//使用SAXBuilder将给定的XML文档读入JDOM Document对象,
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("D:/eclipseCode/edipsegaoji/xmlDom/src/student.xml"));
Element foo = doc.getRootElement();
List allChildren = foo.getChildren();
for (int i = 0; i < allChildren.size(); i++) {
Element e=(Element) allChildren.get(i);
if(e.getName().equals("student")){
System.out.println("name:" +((Element) allChildren.get(i)).getChild("name").getText());
System.out.println("sex:"+ ((Element) allChildren.get(i)).getChild("sex").getText());
List ll=e.getChildren();
System.out.println("ll =" +ll.size());
for (int j = 0; j < ll.size(); j++) {
Element e1=(Element) ll.get(j);
if(e1.getName().equals("lesson")){
System.out.println("lessonName:" +((Element) ll.get(j)).getChild("lessonName").getText());
System.out.println("lessonScore:"+ ((Element) ll.get(j)).getChild("lessonScore").getText());
}
}
}
}
3、dom4j:
File f = new File("D:/eclipseCode/edipsegaoji/xmlDom/src/student.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(f);
Element root = doc.getRootElement();
Element foo;
Element foochild;
System.out.println("XML文件开始解析");
for (Iterator i = root.elementIterator("student"); i.hasNext();) {
foo = (Element) i.next();
System.out.println("name :" + foo.elementText("name"));
System.out.println("sex :" + foo.elementText("sex"));
for (Iterator j= foo.elementIterator("lesson"); j.hasNext();) {
foochild = (Element) j.next();
System.out.print(foochild.elementText("lessonName"));
System.out.println(" :" + foochild.elementText("lessonScore"));
}
}
4、sax:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sax;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
*
* @author 尹彬
*/
public class TestSaxParser {
public static void saxParserTest() {
try {
XMLReader xMLReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
xMLReader.setContentHandler(new ContentHandler() {
private int k;
//DTD中定义的元素名
private static final String ELEMENT_NAME = "name";
private static final String ELEMENT_SEX = "sex";
private static final String ELEMENT_LESSON = "lesson";
private static final String ELEMENT_LESSON_NAME = "lessonName";
private static final String ELEMENT_LESSON_SCORE = "lessonScore";
private static final String ELEMENT_STUDENT = "student";
private static final String ELEMENT_LINE = "breakLine";
//当前元素的数据
private String currentData = "";
private String lessonName = "";
private String lessonScore = "";
/**
* 当其他某一个调用事件发生时,先调用此方法来在文档中定位。
*/
public void setDocumentLocator(Locator locator) {
}
/**
* 在解析整个文档开始时调用
*/
public void startDocument() throws SAXException {
System.out.println("XML文件开始解析");
}
/**
* 在解析整个文档结束时调用
*/
public void endDocument() throws SAXException {
System.out.println("XML文件解析结束");
}
/**
* 在解析命名空间开始时调用
*/
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}
/**
* 在解析命名空间结束时调用
*/
public void endPrefixMapping(String prefix) throws SAXException {
}
/**
* 在解析元素开始时调用
*/
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
}
/**
* 在解析元素结束时调用
* @param namespaceURI
* @param localName 本地名,如student
* @param qName 原始名
*/
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
//System.out.println(++k +" = endElement");
if (localName.equals(ELEMENT_NAME)) {
System.out.println(localName + ":" + currentData);
}
if (localName.equals(ELEMENT_SEX)) {
System.out.println(localName + ":" + currentData);
}
if (localName.equals(ELEMENT_LESSON_NAME)) {
System.out.println(++k + " = lessonName");
this.lessonName = currentData;
}
if (localName.equals(ELEMENT_LESSON_SCORE)) {
System.out.println(++k + " = lessonScore");
this.lessonScore = currentData;
}
if (localName.equals(ELEMENT_LESSON)) {
System.out.println(++k + " = lession");
System.out.println(lessonName + ":" + lessonScore);
}
if (localName.equals(ELEMENT_LINE)) {
System.out.println("------------------------------------");
}
}
/**
* 取得元素数据
*/
public void characters(char[] ch, int start, int length) throws SAXException {
currentData = new String(ch, start, length).trim();
}
/**
* 取得元素数据中的空白
*/
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}
/**
* 在解析到处理指令时,调用此方法。
* 这些处理指令不包括XML的版权指令,它由解析器本身识别。
*/
public void processingInstruction(String target, String data) throws SAXException {
}
/**
* 当未验证解析器忽略实体时调用此方法
*/
public void skippedEntity(String name) throws SAXException {
}
});
xMLReader.parse(TestSaxParser.class.getResource("/") + "student.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : student.xml
Created on : 2010年4月6日, 下午3:01
Author : Administrator
Description:
Purpose of the document follows.
-->
<!DOCTYPE StudentInfo SYSTEM "student.dtd">
<StudentInfo>
<student>
<name>刘伟</name>
<sex>男</sex>
<lesson>
<lessonName>Java初级</lessonName>
<lessonScore>85</lessonScore>
</lesson>
<lesson>
<lessonName>Oracle数据库</lessonName>
<lessonScore>95</lessonScore>
</lesson>
<lesson>
<lessonName>Java中级</lessonName>
<lessonScore>80</lessonScore>
</lesson>
<lesson>
<lessonName>Java高级</lessonName>
<lessonScore>100</lessonScore>
</lesson>
</student>
<breakLine />
<student>
<name>花木兰</name>
<sex>女</sex>
<lesson>
<lessonName>射箭</lessonName>
<lessonScore>100</lessonScore>
</lesson>
<lesson>
<lessonName>柔道</lessonName>
<lessonScore>95</lessonScore>
</lesson>
<lesson>
<lessonName>国学</lessonName>
<lessonScore>80</lessonScore>
</lesson>
<lesson>
<lessonName>烹饪</lessonName>
<lessonScore>60</lessonScore>
</lesson>
<lesson>
<lessonName>游泳</lessonName>
<lessonScore>70</lessonScore>
</lesson>
</student>
<breakLine/>
</StudentInfo>