我想创建一个解析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:Stream已关闭 在java.io.StringReader.ensureOpen(未知来源) 在java.io.StringReader.read(未知来源) at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(Unknown Source) at 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) 在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知来源) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) 在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) - - - - - - - - 链接到 - - - - - - - - - javax.xml.xpath.XPathExpressionException at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source) 在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关闭 在java.io.StringReader.ensureOpen(未知来源) 在java.io.StringReader.read(未知来源) at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(Unknown Source) at 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) 在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(未知来源) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
我不明白为什么我不能再使用&#34; xpath.evaluate(i,docXml)&#34;在循环中。
由于
答案 0 :(得分:4)
您使用相同的InputSource
两次 - 之后InputSource
读取输入一次,输入就会用完。
最简单的方法可能是为每次迭代创建一个新的InputSource
,每次都有一个新的StringReader
。基本上移动这一行:
InputSource docXml = new InputSource(new StringReader(responseBody));
进入循环。
或者,我确信必须有一种方法来针对解析的DOM表示(例如Document
)评估XPath表达式,但是我最近没有做足够的Java XML工作来了解相关的魔术咒语。这样可以避免为每个表达式重新分析XML,这显然很好。