我想创建一个类来解析 XML。这是我的代码:
public class ParserXML {
private String Flux;
private ArrayList<String> EvaluateExpr = new ArrayList<String>();
private ArrayList<String> Result = new ArrayList<String>();
public ParserXML(String url, ArrayList<String> exp) {
this.Flux = url;
this.EvaluateExpr = exp;
this.loadXmlFlux();
}
private void loadXmlFlux() {
HttpClient httpclient = null;
try {
httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(this.Flux);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
InputSource docXml = new InputSource(new StringReader(responseBody));
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
ArrayList<String> result = new ArrayList<String>();
/*
* Looping on all expression to evaluate
*/
for (String i : this.EvaluateExpr) {
try {
result.add(xpath.evaluate(i, docXml));
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
this.Result = result;
} catch (HttpResponseException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally{
if(httpclient!=null) {
httpclient.getConnectionManager().shutdown();
}
}
}
public ArrayList<String> getResult() {
return this.Result;
}
}
当我只有一个表达式要评估时,这个类工作正常。但是,如果我想在 XML 流上评估 2 表达式,我会出错。
java.io.IOException:流在 com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load 的 java.io.StringReader.read(Unknown Source) 处的 java.io.StringReader.ensureOpen(Unknown Source) 处关闭(未知来源)在 com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(未知来源)在 com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(未知来源)在 com .sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache .xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers .DOMParser。解析(未知来源)在 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(未知来源)在 com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(未知来源)在com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) at Common.ParserXML.loadXmlFlux(ParserXML.java:56) at Common.ParserXML.(ParserXML.java:29) at Domaine.Ahref .loadReferringDomains(Ahref.java:46) 在 Domaine.Ahref.(Ahref.java:19) 在 MainApp.checkAhrefs(MainApp.java:199) 在 MainApp.main(MainApp.java:67) ------- -------- 链接到 ------------------ com.sun.org.apache.xpath.internal.jaxp 处的 javax.xml.xpath.XPathExpressionException。 XPathImpl.evaluate(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) at Common.ParserXML。loadXmlFlux(ParserXML.java:56) 在 Common.ParserXML.(ParserXML.java:29) 在 Domaine.Ahref.loadReferringDomains(Ahref.java:46) 在 Domaine.Ahref.(Ahref.java:19) 在 MainApp.checkAhrefs( MainApp.java:199) 在 MainApp.main(MainApp.java:67) 原因:java.io.IOException: Stream closed at java.io.StringReader.ensureOpen(Unknown Source) at java.io.StringReader.read(Unknown来源)在 com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(未知来源)在 com.sun .org.apache.xerces.internal.impl.XMLEntityScanner.skipString(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source) at com.sun.org.apache.xerces .internal.parsers。XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source ) 在 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) 的 com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
我不明白为什么我不能在循环中再次使用“xpath.evaluate(i, docXml)”。
谢谢