將XML文檔中的內(nèi)容轉(zhuǎn)換為JAVA對(duì)象共有4中方式它,它們分別是DOM注益,SAX舶沿,DOM4J 墙杯, JDOM。接下來(lái)我用代碼演示JAVA官方提供我們的兩種方式DOM和SAX括荡。
DOM是JDK自帶的接下來(lái)我來(lái)演示一下怎樣獲取XML文本中的內(nèi)容高镐。以下是xml文件中的內(nèi)容。
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="1508022226" stuid="123">
<name>張三</name>
<age>12</age>
</student>
<student id="1508022223" stuid="456">
<name>李四</name>
<age>16</age>
</student>
</students>
我們通過(guò)DOM的方式來(lái)獲取來(lái)獲取xml文件中的內(nèi)容畸冲。
public class DOMDemo01 {
private static DocumentBuilderFactory documentBuilderFactory ;
private static DocumentBuilder documentBuilder;
private static Document document;
public static void main(String[] args) {
documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
try {
document = documentBuilder.parse("src/main/resources/xml/student.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NodeList parents = document.getElementsByTagName("student");
System.out.println("一共有"+parents.getLength()+"個(gè)節(jié)點(diǎn)");
for(int i=0;i<parents.getLength();i++){
Element students = (Element) parents.item(i);
//獲取父標(biāo)簽中的值
String id = students.getAttribute("id");
String stuid = students.getAttribute("stuid");
System.out.println("id : "+id+" stuid : "+stuid);
//獲取子標(biāo)簽
NodeList childs = students.getChildNodes();
//DOM將空白和文件也看作為節(jié)點(diǎn) // System.out.println(childs.getLength());
//打印student節(jié)點(diǎn)的所有標(biāo)簽
for(int j=0;j<childs.getLength();j++){
//獲取除文本節(jié)點(diǎn)和空格節(jié)點(diǎn)
if(childs.item(j).getNodeType() == Node.ELEMENT_NODE){
Student student = new Student();
System.out.print(childs.item(j).getNodeName() + " : ");
//通過(guò)childs.item(j).getTextContent()也可以獲取節(jié)點(diǎn)內(nèi)容
System.out.println(childs.item(j).getFirstChild().getNodeValue());
}
}
}
}
}
以下是輸出的內(nèi)容
SAX解析XML文件
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
public class SAXDemo {
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
SAXHandle saxHandle = new SAXHandle();
parser.parse("src/main/resources/xml/student.xml",saxHandle);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXHandle extends DefaultHandler {
/*
* 用來(lái)表示解析的開始
*/
@Override
public void startDocument () throws SAXException {
super.startDocument();
System.out.println("解析標(biāo)簽開始");
}
@Override
public void endDocument () throws SAXException {
super.startDocument();
System.out.println("解析標(biāo)簽結(jié)束");
}
@Override
public void startElement (String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri,localName,qName,attributes);
if(qName.equals("student") ){
int length = attributes.getLength();
for(int i=0;i<length;i++){
System.out.print(attributes.getQName(i)+" : ");
System.out.print(attributes.getValue(i)+" ");
}
System.out.println();
}else if(!qName.equals("student")){
System.out.print("節(jié)點(diǎn)名 : "+qName +" ");
}
}
/*
* 用來(lái)表示解析的結(jié)束
*/
@Override
public void endElement (String uri, String localName, String qName)
throws SAXException {
super.endElement(uri,localName,qName);
}
@Override
public void characters (char ch[], int start, int length)
throws SAXException {
String value = new String(ch,start,length);
//去掉換行和空制符
if(!value.trim().equals("")){
System.out.println(value);
}
}
}
運(yùn)行結(jié)果
總結(jié):DOM解析將文件的內(nèi)容全部加載在內(nèi)存中并且形成一個(gè)DOM樹嫉髓,如果XML非常大,則DOM很浪費(fèi)時(shí)間邑闲。SAX是基于事件的解析算行,會(huì)一步一步的判斷每一行的XML文件中的內(nèi)容,執(zhí)行開始標(biāo)簽觸發(fā)startDocument()方法苫耸,每走到結(jié)束標(biāo)簽都會(huì)觸發(fā)一個(gè)endDocument方法州邢。執(zhí)行完最后標(biāo)簽會(huì)執(zhí)行endDocument方法。
DOM
優(yōu)點(diǎn):形成DOM樹褪子,實(shí)現(xiàn)簡(jiǎn)單量淌。
缺點(diǎn): 當(dāng)XML文件非常大時(shí),對(duì)內(nèi)存消耗比較大嫌褪,容易影響解析呀枢,容易造成內(nèi)存溢出。
SAX
優(yōu)點(diǎn):采用事件的驅(qū)動(dòng)模式笼痛,不需要關(guān)系賦值節(jié)點(diǎn)裙秋,按行進(jìn)行解析琅拌。
缺點(diǎn):不易編碼。