java解析xml常用的幾種方式總結(jié)
各種方法都用過?,F(xiàn)在總結(jié)一下。 經(jīng)常記不住,要找資料?,F(xiàn)在總結(jié)一下。
xml 文件如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
<aa>
<bb>
<cc>ccccc</cc>
</bb>
</aa>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
package sort;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class D2 {
/**
* 直接使用DOM解析
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
DocumentBuilder sb = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document root = sb.parse(D2.class.getClassLoader().getResourceAsStream("NewFile.xml"));
System.out.println(root.getChildNodes().item(0).getNodeName());
}
}
package sort;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
public class D {
/**
* 使用SAX解析
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactoryImpl.newInstance();
SAXParser parser = factory.newSAXParser() ;
parser.parse(D.class.getClassLoader().getResourceAsStream("NewFile.xml"),
new DefaultHandler(){
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("characters");
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("endDocument");
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
// TODO Auto-generated method stub
System.out.println("endElement");
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("startDocument");
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
// TODO Auto-generated method stub
System.out.println("startElement");
}
}) ;
}
}
package sort;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
public class D3 {
/**
* 使用XMLStream解析
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
XMLInputFactory xmlFactor = XMLInputFactory.newFactory();
XMLStreamReader reader =
xmlFactor.createXMLStreamReader(D3.class.getClassLoader().getResourceAsStream("NewFile.xml"));
while(reader.hasNext()){
int point = reader.next() ;
switch(point){
case XMLStreamReader.START_ELEMENT :
System.out.println("start_element");
case XMLStreamReader.END_ELEMENT :
// do something...
}
}
}
}
package sort;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
/**
* 使用DOM4j XPATH解析XML (需要加入依賴jar文件)
* @author zhoufeng
*
*/
public class D4 {
public static void main(String[] args) throws Exception{
SAXReader reader = new SAXReader() ;
Document root = reader.read(D4.class.getClassLoader().getResourceAsStream("NewFile.xml"));
/* 選擇所有的cc節(jié)點 */
System.out.println(root.selectNodes("http://cc").size());;
/*選擇所有的book節(jié)點,并且有子節(jié)點author的*/
System.out.println((root.selectNodes("http://book[author]").size()));;
/* 選擇所有book節(jié)點,并且有屬性category的 */
System.out.println((root.selectNodes("http://book[@category]").size()));;
/* 選擇所有book節(jié)點,并且有子節(jié)點author值為James McGovern ,并且還有category屬性節(jié)點值為WEB 下面的price節(jié)點*/
System.out.println(root.selectNodes("http://book[author='James McGovern'][@category='WEB']/price").size());;
}
}
相關(guān)文章
SpringCloud Alibaba Seata (收藏版)
Seata是一款開源的分布式事務(wù)解決方案,致力于在微服務(wù)架構(gòu)在提供高性能和簡單一樣的分布式事務(wù)服務(wù)。這篇文章主要介紹了SpringCloud Alibaba Seata 的相關(guān)知識,需要的朋友可以參考下2020-10-10通過Feign進(jìn)行調(diào)用@FeignClient?找不到的解決方案
這篇文章主要介紹了通過Feign進(jìn)行調(diào)用@FeignClient?找不到的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java中線程狀態(tài)+線程安全問題+synchronized的用法詳解
這篇文章主要介紹了Java中線程狀態(tài)+線程安全問題+synchronized的用法詳解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗詳解
這篇文章主要介紹了Java之Rsync并發(fā)遷移數(shù)據(jù)并校驗詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08httpclient的disableConnectionState方法工作流程
這篇文章主要為大家介紹了httpclient的disableConnectionState方法工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11