package com.imooc.domtest.test;import java.io.IOException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;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) { DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); try{ DocumentBuilder db=dbf.newDocumentBuilder(); Document document=db.parse("books.xml"); NodeList bookList=document.getElementsByTagName("book"); System.out.println("一共有"+bookList.getLength()+"书本"); for(int i=0;i<bookList.getLength();i++){ System.out.println("====下面开始遍历第"+(i+1)+"本书内容===="); Node book=bookList.item(i); NamedNodeMap attrs=book.getAttributes(); System.out.println("第"+(i+1)+"本书共有"+attrs.getLength()+"个属性"); for(int j=0;j<attrs.getLength();j++){ Node attr=attrs.item(j); System.out.print("属性名:"+attr.getNodeName()); System.out.println("---属性值"+attr.getNodeValue()); } System.out.println("====下面结束遍历第"+(i+1)+"本书内容===="); } }catch(ParserConfigurationException e){ e.printStackTrace(); }catch(SAXException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } }}错误提示:[Fatal Error] books.xml:13:37: The element type "language" must be terminated by the matching end-tag "</language>".org.xml.sax.SAXParseException: The element type "language" must be terminated by the matching end-tag "</language>". at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:180) at com.imooc.domtest.test.DomTest.main(DomTest.java:24)
添加回答
举报
0/150
提交
取消