Java 我怎样才能得到“的结果?”;至于;活动
我想做的是获取从以下类生成的Result:Java 我怎样才能得到“的结果?”;至于;活动,java,xpath,return,Java,Xpath,Return,我想做的是获取从以下类生成的Result: public class QueryXML { public String query; public QueryXML(String query){ this.query=query; } public void query() throws ParserConfigurationException, SAXException,IOException,XPathExpressionException { // Standar
public class QueryXML {
public String query;
public QueryXML(String query){
this.query=query;
}
public void query() throws ParserConfigurationException, SAXException,IOException,XPathExpressionException {
// Standard of reading an XML file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
XPathExpression expr = null;
builder = factory.newDocumentBuilder();
doc = builder.parse("C:data.xml");
// create an XPathFactory
XPathFactory xFactory = XPathFactory.newInstance();
// create an XPath object
XPath xpath = xFactory.newXPath();
// Compile the XPath expression
expr = xpath.compile(query);
// Run the query and get a nodeset
Object result = expr.evaluate(doc, XPathConstants.NODESET);
// Cast the result to a DOM NodeList
NodeList nodes = (NodeList) result;
for (int i=0; i<nodes.getLength();i++){
System.out.print(nodes.item(i).getNodeValue());
}
}
}
公共类QueryXML{
公共字符串查询;
公共查询XML(字符串查询){
this.query=query;
}
public void query()抛出ParserConfiguration异常、SAXException、IOException、XPathExpressionException{
//读取XML文件的标准
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
文档生成器;
单据单据=空;
XPathExpression expr=null;
builder=factory.newDocumentBuilder();
doc=builder.parse(“C:data.xml”);
//创建XPathFactory
XPathFactory xFactory=XPathFactory.newInstance();
//创建XPath对象
XPath=xFactory.newXPath();
//编译XPath表达式
expr=xpath.compile(查询);
//运行查询并获取节点集
Object result=expr.evaluate(doc,XPathConstants.NODESET);
//将结果强制转换为DOM节点列表
节点列表节点=(节点列表)结果;
对于(int i=0;i将System.out.print(nodes.item(i).getNodeValue());
替换为System.out.print(nodes.item(0).getFirstChild().getNodeValue());
,同时,当标记像这样时,还需要检查getFirstChild
的空值
这些类工作正常,我可以在控制台中看到结果,但我想将“process.query()”的rsult分配给一个变量,以便在此过程之后使用它
因此,您必须将函数vom“void”的返回值类型更改为f.e.“org.w3c.dom.Document”,您必须更改函数,使其返回有效的xml文档。首先,您需要从query()
方法返回结果:
public NodeList query() throws ParserConfigurationException,
SAXException,IOException,XPathExpressionException {
...
// Cast the result to a DOM NodeList
NodeList nodes = (NodeList) result;
return nodes;
}
然后,您可以将结果添加到数组中以供以后处理:
public static void main(String[] args) throws XPathExpressionException,
ParserConfigurationException, SAXException, IOException {
String Queries[]={
"//Employees/Employee/Firstname/City/@value",
"//Employees/Employee/Firstname/Lastname/@value"
};
List<NodeList> results = new ArrayList<NodeList>();
for (int x =0; x < Queries.length; x++){
String query = Queries[x];
QueryXML process = new QueryXML(query);
results.add(process.query());
}
}
publicstaticvoidmain(String[]args)抛出XPathExpressionException,
ParserConfiguration异常、SAXException、IOException{
字符串查询[]={
“//Employees/Employee/Firstname/City/@value”,
“//Employees/Employee/Firstname/Lastname/@value”
};
列表结果=新建ArrayList();
for(int x=0;x
Hi当我只有一个查询时,它似乎可以工作,但是当我尝试使用多个查询时,我会收到下一个错误:线程“main”中出现异常java.lang.OutOfMemoryError:java heap spaceOk,修复。我已经提高了内存量,现在可以工作了。也许还有其他方法可以修复它,比如在处理第二个查询之前清理内存之类的,但我不知道。非常感谢。很高兴你能让它工作。如果你在找到每个结果时处理它们,而不是添加所有结果将结果保存到列表中,这样您的程序将消耗更少的内存。或者,您可以序列化结果并将其存储在磁盘上。您的结果列表可以这样存储文件引用。
public static void main(String[] args) throws XPathExpressionException,
ParserConfigurationException, SAXException, IOException {
String Queries[]={
"//Employees/Employee/Firstname/City/@value",
"//Employees/Employee/Firstname/Lastname/@value"
};
List<NodeList> results = new ArrayList<NodeList>();
for (int x =0; x < Queries.length; x++){
String query = Queries[x];
QueryXML process = new QueryXML(query);
results.add(process.query());
}
}