資源簡介
文檔中有代碼案例,詳細講解了使用java讀取xml,并附帶有關于xml的dtd講解,里面包含詳細的文檔和代碼案例。和讀取xml所需的jar。

代碼片段和文件信息
package?com.bzjy.test;
import?org.junit.Test;
import?org.w3c.dom.*;
import?javax.xml.parsers.*;
import?java.io.*;
public?class?JavaReadxml?{
//?Document可以看作是xml在內存中的一個鏡像那么一旦獲取這個Document?就意味著可以通過對
//?內存的操作來實現對xml的操作首先第一步獲取xml相關的Document
private?Document?doc?=?null;
public?void?init(String?xmlFile)?throws?Exception?{
//?很明顯該類是一個單例先獲取產生DocumentBuilder工廠
//?的工廠在通過這個工廠產生一個DocumentBuilder
//?DocumentBuilder就是用來產生Document的
DocumentBuilderFactory?dbf?=?DocumentBuilderFactory.newInstance();
DocumentBuilder?db?=?dbf.newDocumentBuilder();
//?這個Document就是一個xml文件在內存中的鏡像
doc?=?db.parse(new?File(xmlFile));
}
//?該方法負責把xml文件的內容顯示出來
public?void?viewxml(String?xmlFile)?throws?Exception?{
this.init(xmlFile);
//?在xml文件里只有一個根元素先把根元素拿出來看看
Element?element?=?doc.getDocumentElement();
System.out.println(“根元素為:“?+?element.getTagName());
NodeList?nodeList?=?doc.getElementsByTagName(“person“);
System.out.println(“book節點鏈的長度:“?+?nodeList.getLength());
Node?fatherNode?=?nodeList.item(0);
System.out.println(“父節點為:“?+?fatherNode.getNodeName());
//?把父節點的屬性拿出來
NamedNodeMap?attributes?=?fatherNode.getAttributes();
for?(int?i?=?0;?i? Node?attribute?=?attributes.item(i);
System.out.println(“book的屬性名為:“?+?attribute.getNodeName()
+?“?相對應的屬性值為:“?+?attribute.getNodeValue());
}
NodeList?childNodes?=?fatherNode.getChildNodes();
System.out.println(childNodes.getLength());
for?(int?j?=?0;?j? Node?childNode?=?childNodes.item(j);
//?如果這個節點屬于Element?再進行取值
if?(childNode?instanceof?Element)?{
//?System.out.println(“子節點名為:“+childNode.getNodeName()+“相對應的值為“+childNode.getFirstChild().getNodeValue());
System.out.println(“子節點名為:“?+?childNode.getNodeName()
+?“相對應的值為“?+?childNode.getFirstChild().getNodeValue());
}
}
}
@Test
public?void?test_xml()?throws?Exception?{
JavaReadxml?parse?=?new?JavaReadxml();
//?我的xml文件放置在項目下
parse.viewxml(“person.xml“);
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????23969??2017-01-06?11:52??dtd-xm
?????文件????????607??2017-01-06?10:15??dtd-xm
?????文件????????297??2017-06-05?12:54??dtd-xm
?????文件???????1535??2017-01-06?11:29??dtd-xm
?????文件????????500??2017-01-06?10:13??dtd-xm
?????文件???????1009??2017-01-06?11:30??dtd-xm
?????文件????????364??2017-01-06?10:13??dtd-xm
?????文件????????462??2017-01-06?11:30??dtd-xm
?????文件????????252??2017-01-06?10:13??dtd-xm
?????文件?????????49??2017-01-06?10:13??dtd-xm
?????文件??????????6??2017-01-06?10:13??dtd-xm
?????文件????????365??2017-06-05?12:55??dtd-xm
?????文件????????387??2017-06-05?13:13??dtd-xm
?????文件????????575??2017-06-05?13:13??dtd-xm
?????文件???????2300??2017-06-05?12:56??dtd-xm
?????文件????????829??2017-01-06?10:13??dtd-xm
?????文件?????????36??2017-01-06?10:13??dtd-xm
?????文件???????3043??2017-06-05?12:56??dtd-xm
?????文件????????462??2017-01-06?11:29??dtd-xm
?????文件?????930816??2017-06-05?13:08??dtd-xm
?????文件??????18960??2017-01-06?11:49??dtd-xm
?????文件???????1411??2011-02-28?16:42??dtd-xm
?????文件????????420??2011-02-28?16:42??dtd-xm
?????文件????????381??2011-02-28?16:42??dtd-xm
?????文件????????944??2011-02-28?16:42??dtd-xm
?????文件????????100??2011-02-28?16:42??dtd-xm
?????文件???????1976??2011-02-28?16:42??dtd-xm
?????文件????????865??2011-02-28?16:42??dtd-xm
?????文件????????674??2011-02-28?16:42??dtd-xm
?????目錄??????????0??2018-03-15?12:50??dtd-xm
............此處省略29個文件信息
評論
共有 條評論