在读取XML数据的时候可以采用两种实现,可以灵活应用。
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> </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>
实现一:
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 readXML2(String xmlFile){ try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(xmlFile); Element root = doc.getDocumentElement();//root 指向bookstore节点 NodeList nls = root.getChildNodes();//nls指向bookstore的子节点book if(nls!= null){ for(int i=0;i<nls.getLength();i++){ Node nd = nls.item(i);//输出每个book节点的category属性 if(nd.getNodeType() == Node.ELEMENT_NODE){ System.out.println("book:category:" + nd.getAttributes().getNamedItem("category").getNodeValue()); } //以下遍历book节点的所有子节点 for(Node nde = nd.getFirstChild();nde!=null; nde = nde.getNextSibling()){ if(nde.getNodeType() == Node.ELEMENT_NODE){ //输出title节点值 if(nde.getNodeName().equals("title")){ System.out.println("----title:" + nde.getFirstChild().getNodeValue()); } //输出author节点值 if(nde.getNodeName().equals("author")){ System.out.println("----author:" + nde.getFirstChild().getNodeValue()); } //输出year节点值 if(nde.getNodeName().equals("year")){ System.out.println("----year:" + nde.getFirstChild().getNodeValue()); } //输出price节点值 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 main(String[] args) { String xmlFile = "E:\\workspace_g2\\TT\\src、\\books.xml"; readXML2(xmlFile); }
相关文章参见:
http://www.blogjava.net/lcs/archive/2007/11/21/162088.html
http://achievo-bruce-126-com.iteye.com/blog/221480
http://www.91linux.com/html/article/program/java/20070901/6639.html
http://blog.csdn.net/ydsakyclguozi/archive/2008/04/30/2347575.aspx
http://blog.csdn.net/java2000_net/archive/2008/11/06/3241167.aspx