JAVA语言利用DOM解析XML文件
利用DOM解析XML文件的时候,会把XML文件预读的内存中,如果是比较大的XML文件或者是移动设备的话,不建议使用此方法
下面是我解析一个XML文件的详细代码
- import Java.io.InputStream;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import com.fujitsu.cn.model.Patent;
- import com.fujitsu.cn.util.LogHelper;
- /**
- * 采用DOM解析xml文件
- */
- public class DOMDetailService {
- public static Patent getPatent(InputStream inStream) throws Exception {
- Patent patent = new Patent();
- LogHelper.v("domservice");
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document document = db.parse(inStream);
- //通过getDocumentElement()方法获得该xml文件的根节点
- Element root = document.getDocumentElement();
- //获得以 details为标签对的所有节点对的列表<details></details>
- NodeList detailNodes = root.getElementsByTagName("details");
- //由于本xml文件中只有一个这样的标签对,通过item(i)得到该元素,并且进行强制转化
- Element patentElement = (Element) detailNodes.item(0);
- //获得该标签下面的所有标签的节点列表
- NodeList childList = patentElement.getChildNodes();
- //对获得到的节点列表进行遍历 getLength()可以得到NodeList列表的长度
- for(int i=0; i<childList.getLength();i++){
- //遍历所有的属性节点 nodelist.item(i)返回一个Node节点
- Node patentAttribut = (Node)childList.item(i);
- //由于可能会出现空的节点,对该节点进行判断,判断该节点是不是该xml中的元素
- //Node.ELEMENT_NODE的值为1,表示该节点是一个Element
- if(patentAttribut.getNodeType() == Node.ELEMENT_NODE){
- //通过getNodeName()得到该Node的名字
- //例如遍历到<type>3</type>,取值以后就是 type
- String tag = patentAttribut.getNodeName();
- //getFirstChild()表示获得该节点下面的第一个孩子,getNodeValue()表示得到该孩子的值
- //例如遍历到<type>3</type>通过该函数得到的值为3
- String val = patentAttribut.getFirstChild().getNodeValue();
- if("othercontents".equals(tag)){
- patent.setOthercontents(val);
- }else
- if("publiccountry".equals(tag)){
- patent.setCountry(val);
- }else
- if("type".equals(tag)){
- patent.setType(val);
- }
- else
- if("title".equals(tag)){
- patent.setTitle(val);
- }
- else
- if("IPC".equals(tag)){
- patent.setIpc_number(val);
- }
- else
- if("status".equals(tag)){
- patent.setStatus(val);
- }
- else
- if("pagenum".equals(tag)){
- patent.setPagenum(val);
- }
- else
- if("submitid".equals(tag)){
- patent.setSubmit_number(val);
- }
- else
- if("inventors".equals(tag)){
- patent.setInventor(val);
- }
- else
- if("agent".equals(tag)){
- patent.setAgent(val);
- }
- else
- if("claims".equals(tag)){
- patent.setClaims(val);
- }
- else
- if("applicant".equals(tag)){
- patent.setProposer(val);
- }
- }
- }
- inStream.close();
- return patent;
- }
- }
对该单元进行测试的测试类如下所示:
- import java.io.InputStream;
- import java.util.List;
- public class Test {
- public static void main(String[] args) throws Exception {
- InputStream inStream = new MainTest().getClass().getClassLoader()
- .getResourceAsStream("detail.xml");
- Patent p = DOMDetailService.getPatent(inStream);
- System.out.println(p.getOthercontents());
- }
- }
解析的XML文件由于内容太多,已经将其中的内容精简
- <?xml version="1.0" encoding="utf-8"?>
- <response>
- <details>
- <othercontents>1</othercontents>
- <publiccountry>2</publiccountry>
- <type>3</type>
- <title>4</title>
- <IPC>5</IPC>
- <status>6</status>
- <pagenum>7</pagenum>
- <submitid>8</submitid>
- <submitdate>9</submitdate>
- <inventors>10</inventors>
- <agent>11</agent>
- <claims>12</claims>
- <applicant>13</applicant>
- </details>
- </response>
用到的Model如下所示
- public class Patent {
- private String title;
- private String country;
- private String type;
- private String status;
- private String ipc_number;
- private String patent_pages;
- private String submit_number;
- private String submit_data;
- private String proposer;
- private String inventor;
- private String agent;
- private String othercontents;
- private String pagenum;
- private String articleid;
- private String claims;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getStatus() {
- return status;
- }
- public void setStatus(String status) {
- this.status = status;
- }
- public String getPatent_pages() {
- return patent_pages;
- }
- public void setPatent_pages(String patentPages) {
- patent_pages = patentPages;
- }
- public String getSubmit_number() {
- return submit_number;
- }
- public void setSubmit_number(String submitNumber) {
- submit_number = submitNumber;
- }
- public String getSubmit_data() {
- return submit_data;
- }
- public void setSubmit_data(String submitData) {
- submit_data = submitData;
- }
- public String getProposer() {
- return proposer;
- }
- public void setProposer(String proposer) {
- this.proposer = proposer;
- }
- public String getInventor() {
- return inventor;
- }
- public void setInventor(String inventor) {
- this.inventor = inventor;
- }
- public String getAgent() {
- return agent;
- }
- public void setAgent(String agent) {
- this.agent = agent;
- }
- public String getIpc_number() {
- return ipc_number;
- }
- public void setIpc_number(String ipcNumber) {
- ipc_number = ipcNumber;
- }
- public String getOthercontents() {
- return othercontents;
- }
- public void setOthercontents(String othercontents) {
- this.othercontents = othercontents;
- }
- public String getPagenum() {
- return pagenum;
- }
- public void setPagenum(String pagenum) {
- this.pagenum = pagenum;
- }
- public String getArticleid() {
- return articleid;
- }
- public void setArticleid(String articleid) {
- this.articleid = articleid;
- }
- public String getClaims() {
- return claims;
- }
- public void setClaims(String claims) {
- this.claims = claims;
- }
- }