在讀取XML數(shù)據(jù)的時候可以采用兩種實現(xiàn),可以靈活應(yīng)用。
XML文件為:
view plaincopy to clipboardprint?
<?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>
</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>
<year>2003</year>
<price>49.99</price>
</book>
<book category="EEWEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<?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>
</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>
<year>2003</year>
<price>49.99</price>
</book>
<book category="EEWEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
實現(xiàn)一:
view plaincopy to clipboardprint?
public static void readXML(String xmlFile){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();
NodeList ndBooks = root.getElementsByTagName("book");
NodeList ndTitles = root.getElementsByTagName("title");
NodeList ndAuthors = root.getElementsByTagName("author");
NodeList ndYears = root.getElementsByTagName("year");
NodeList ndPrices = root.getElementsByTagName("price");
if(ndBooks != null){
for(int i=0;i<ndBooks.getLength();i++){
Node ndBook = ndBooks.item(i);
Node ndTitle = ndTitles.item(i);
Node ndAuthor = ndAuthors.item(i);
Node ndYear = ndYears.item(i);
Node ndPrice = ndPrices.item(i);
System.out.println(ndBook.getAttributes().getNamedItem("category").getNodeValue());
System.out.println(ndTitle.getAttributes().getNamedItem("lang").getNodeValue());
System.out.println(ndTitle.getFirstChild().getNodeValue());
System.out.println(ndAuthor.getFirstChild().getNodeValue());
System.out.println(ndYear.getFirstChild().getNodeValue());
System.out.println(ndPrice.getFirstChild().getNodeValue());
System.out.println("--------------------------------");
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readXML(String xmlFile){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();
NodeList ndBooks = root.getElementsByTagName("book");
NodeList ndTitles = root.getElementsByTagName("title");
NodeList ndAuthors = root.getElementsByTagName("author");
NodeList ndYears = root.getElementsByTagName("year");
NodeList ndPrices = root.getElementsByTagName("price");
if(ndBooks != null){
for(int i=0;i<ndBooks.getLength();i++){
Node ndBook = ndBooks.item(i);
Node ndTitle = ndTitles.item(i);
Node ndAuthor = ndAuthors.item(i);
Node ndYear = ndYears.item(i);
Node ndPrice = ndPrices.item(i);
System.out.println(ndBook.getAttributes().getNamedItem("category").getNodeValue());
System.out.println(ndTitle.getAttributes().getNamedItem("lang").getNodeValue());
System.out.println(ndTitle.getFirstChild().getNodeValue());
System.out.println(ndAuthor.getFirstChild().getNodeValue());
System.out.println(ndYear.getFirstChild().getNodeValue());
System.out.println(ndPrice.getFirstChild().getNodeValue());
System.out.println("--------------------------------");
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
實現(xiàn)二:
view plaincopy to clipboardprint?
public static void readXML2(String xmlFile){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();//root 指向bookstore節(jié)點
NodeList nls = root.getChildNodes();//nls指向bookstore的子節(jié)點book
if(nls!= null){
for(int i=0;i<nls.getLength();i++){
Node nd = nls.item(i);//輸出每個book節(jié)點的category屬性
if(nd.getNodeType() == Node.ELEMENT_NODE){
System.out.println("book:category:" + nd.getAttributes().getNamedItem("category").getNodeValue());
}
//以下遍歷book節(jié)點的所有子節(jié)點
for(Node nde = nd.getFirstChild();nde!=null; nde = nde.getNextSibling()){
if(nde.getNodeType() == Node.ELEMENT_NODE){
//輸出title節(jié)點值
if(nde.getNodeName().equals("title")){
System.out.println("----title:" + nde.getFirstChild().getNodeValue());
}
//輸出author節(jié)點值
if(nde.getNodeName().equals("author")){
System.out.println("----author:" + nde.getFirstChild().getNodeValue());
}
//輸出year節(jié)點值
if(nde.getNodeName().equals("year")){
System.out.println("----year:" + nde.getFirstChild().getNodeValue());
}
//輸出price節(jié)點值
if(nde.getNodeName().equals("price")){
System.out.println("----price:" + nde.getFirstChild().getNodeValue());
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readXML2(String xmlFile){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();//root 指向bookstore節(jié)點
NodeList nls = root.getChildNodes();//nls指向bookstore的子節(jié)點book
if(nls!= null){
for(int i=0;i<nls.getLength();i++){
Node nd = nls.item(i);//輸出每個book節(jié)點的category屬性
if(nd.getNodeType() == Node.ELEMENT_NODE){
System.out.println("book:category:" + nd.getAttributes().getNamedItem("category").getNodeValue());
}
//以下遍歷book節(jié)點的所有子節(jié)點
for(Node nde = nd.getFirstChild();nde!=null; nde = nde.getNextSibling()){
if(nde.getNodeType() == Node.ELEMENT_NODE){
//輸出title節(jié)點值
if(nde.getNodeName().equals("title")){
System.out.println("----title:" + nde.getFirstChild().getNodeValue());
}
//輸出author節(jié)點值
if(nde.getNodeName().equals("author")){
System.out.println("----author:" + nde.getFirstChild().getNodeValue());
}
//輸出year節(jié)點值
if(nde.getNodeName().equals("year")){
System.out.println("----year:" + nde.getFirstChild().getNodeValue());
}
//輸出price節(jié)點值
if(nde.getNodeName().equals("price")){
System.out.println("----price:" + nde.getFirstChild().getNodeValue());
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
第二種方法更可取些。注意理解
調(diào)用方法:
view plaincopy to clipboardprint?
public static void main(String[] args) {
String xmlFile = "E:\\workspace_g2\\TT\\src、\\books.xml";
readXML2(xmlFile);
}
public static void main(String[] args) {
String xmlFile = "E:\\workspace_g2\\TT\\src、\\books.xml";
readXML2(xmlFile);
}