代码运行问题
package com.imooc.dmtest.test;
import java.io.*;
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的对象
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//创建一个DocumentBuilder对象
try {
DocumentBuilder db=dbf.newDocumentBuilder();
//通过DocumentBuilder对象的parse方法加载bookstore.xml文件到当前项目下
Document document=db.parse("bookstore.xml");
//获取所有book的节点
NodeList bookList=document.getElementsByTagName("book");
//通过nodeList中的getLength()方法获取book的长度
System.out.println("一共有:"+bookList.getLength()+"本书");
//遍历每一个book节点
for(int i=0;i<bookList.getLength();i++){
System.out.println("=========下面开始遍历第"+(i+1)+"本书的内容=========");
//通过bookList.item()方法获取每一个节点
Node book=bookList.item(i);
//获取book节点所有属性集合
NamedNodeMap attrs=book.getAttributes();
System.out.println("第"+(i+1)+"本书共有"+attrs.getLength()+"个属性");
//遍历book的属性
for(int j=0;j<attrs.getLength();j++){
//通过item(i)方法获取book节点的某一个属性
Node attr=attrs.item(j);
//获取属性名
System.out.print("属性名:"+attr.getNodeName());
//获取属性值
System.out.println("--属性值:"+attr.getNodeValue());
}
System.out.println("=========结束遍历第"+(i+1)+"本书的内容=========");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
代码是按照老师讲的敲上去的,运行结果如下:
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效。
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:557)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.peekChar(XMLEntityScanner.java:497)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2710)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
at com.imooc.dmtest.test.DOMTest.main(DOMTest.java:24)
有没有遇到相同问题,并且解决的同学?