stax 解析xml文件时老是报错,
我今天学习了用stax解析xml文件,怎么老是报这个错误,我给大家好好看看一下我的代码
package com.daijianwei.stax;
import
java.io.IOException;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.junit.Test;
public class TestStax {
@Test
public void test01() {
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()) {
System.out.println(reader.next());
}
} catch (XMLStreamException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
------解决方案--------------------应该是你读取不到books.xml造成的,books.xml放到src根目录看看。