JAVA DOM解析XML文件過程詳解
這篇文章主要介紹了JAVA DOM解析XML文件過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
代碼如下
import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Domtest {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//創(chuàng)建一個DocumentBuilderFactory對象
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//創(chuàng)建一個Doucumentbuild對象
DocumentBuilder db=dbf.newDocumentBuilder();
//解析對應(yīng)的xml文件
Document doc=db.parse("tes.xml");
//根據(jù)標簽名獲取Node節(jié)點list
NodeList nodelist=doc.getElementsByTagName("book");
System.out.println("共有"+nodelist.getLength()+"本書");
//遍歷每一個book節(jié)點
for(int i=0;i<nodelist.getLength();i++) {
System.out.println("第"+i+"本書");
//獲取個book節(jié)點
//使用Node類型獲取book
Node book=nodelist.item(i);
System.out.println("Name: "+book.getNodeName()+" Value: "+book.getNodeValue()+" Type: "+book.getNodeType());
//獲取Node節(jié)點中的屬性
NamedNodeMap attrs= book.getAttributes();
//遍歷獲取屬性
for(int j=0;j<attrs.getLength();j++) {
Node x=attrs.item(j);
//System.out.println(x.getNodeName()+" "+x.getNodeValue()+" "+x.getNodeType());
}
//使用Element對象獲取節(jié)點
Element node =(Element) nodelist.item(i);
//使用Element對象下的getAttribute方法可以獲取指定名字的屬性值
String id=node.getAttribute("id");
System.out.println(id);
String type=node.getAttribute("type");
System.out.println(type);
//使用Node節(jié)點下的getChildNode可以獲取Nodelist數(shù)組,以此進行循環(huán)解析
NodeList childnode=book.getChildNodes();
for(int j=0;j<childnode.getLength();j++) {//getLength后會獲取9個節(jié)點,因為text類型也算節(jié)點,一個<name>……</name>算一個節(jié)點,所以共有9個節(jié)點,而這些節(jié)點中,只有對象節(jié)點是我們需要的
Node x=childnode.item(j);
if(x.getNodeType()==Node.ELEMENT_NODE){//當(dāng)節(jié)點類型為Element時,獲取該節(jié)點
//獲取element類型的節(jié)點名
System.out.println("節(jié)點"+j+"的名字:"+x.getNodeName()+" 值:/"+x.getLastChild().getNodeValue()+"/種類為"+x.getLastChild().getNodeType());//<name>xyz<name>,xyz屬于<name>的子節(jié)點,使用getfirstChild或getLastNode效果相同
System.out.println("節(jié)點"+j+"的名字:"+x.getNodeName()+" 值:/"+x.getTextContent()+"/種類為"+x.getNodeType());//getTextContent方法可以獲取節(jié)點中所有的text內(nèi)容 將<name>xyz</name>改為<name><a>123</a>xyz</name>,會獲取到xyz123
}
}
}
}
}
//為了將獲取到的xml文件中內(nèi)容保存下來,可以將內(nèi)容保存到對象數(shù)組中一次來存儲數(shù)據(jù)
<?xml version="1.0" encoding="UTF-8" ?> <Bookstore> <book id="1" type="text"> <name>冰與火之歌</name> <author>喬治馬丁</author> <year>2014</year> <price>80</price> </book> <book id="2"> <name>安徒生童話</name> <year>2004</year> <price>79</price> <language>English</language> </book> </Bookstore>

注意點
1 空白換行符也算節(jié)點,所以遍歷節(jié)點時需要注意這些無用的節(jié)點會混在list中
2 text類節(jié)點返回Name值都是#text,而Element類節(jié)點返回value值都是null,需要注意
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringAOP+RabbitMQ+WebSocket實戰(zhàn)詳解
這篇文章主要介紹了SpringAOP+RabbitMQ+WebSocket實戰(zhàn)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
詳解Spring bean的注解注入之@Autowired的原理及使用
之前講過bean注入是什么,也使用了xml的配置文件進行bean注入,這也是Spring的最原始的注入方式(xml注入).本文主要講解的注解有以下幾個:@Autowired、 @Service、@Repository、@Controller 、@Component、@Bean、@Configuration、@Resource ,需要的朋友可以參考下2021-06-06
SpringBoot整合EasyCaptcha實現(xiàn)圖形驗證碼功能
這篇文章主要介紹了SpringBoot整合EasyCaptcha實現(xiàn)圖形驗證碼功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02
SpringBoot修改內(nèi)置tomcat版本的操作步驟
生產(chǎn)環(huán)境使用的外部部署Tomcat還是內(nèi)置Tomcat由于版本安全漏洞,往往需要升級到指定的安全版本,本文演示一下SpringBoot升級內(nèi)置的Tomcat版本,感興趣的小伙伴跟著小編一起來看看吧2024-07-07

