从http url获取xml文档,并且打印文档完美无缺(System.out.println(xmlResult)),但是当我将字符串转换为xml文档时,它会产生空指针异常
Java代码
public static void main(String[] args) throws MalformedURLException, IOException, ParserConfigurationException, SAXException {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL url = new URL("http://ops.epo.org/3.1/rest-services/published-data/publication/epodoc/US7039403/biblio");
HttpURLConnection epo = (HttpURLConnection) url.openConnection();
epo.setRequestMethod("GET");
epo.setRequestProperty("ContentType", "text/xml;charset=\"utf-8\"");
epo.setRequestProperty("Accept", "text/xml");
InputStream inputstream = epo.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream,"UTF-8"));
String xmlResult = "";
while ((xmlResult = bufferedReader.readLine()) != null) {
}
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(xmlResult));
Document xmlDocument = documentBuilder.parse(inputSource);
System.out.println("Root element :" + xmlDocument.getDocumentElement().getNodeName());
}
异常详情
Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:50)
at ExtractBiblio.Teser.main(Teser.java:62)
Line:inputSource.setCharacterStream(new StringReader(xmlResult));
帮我解决。