私がやりたかったのは、次のクラスから生成された結果を取得することです:
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());
}
}
}
このクラスは、この別のクラスから呼び出されます。
public class FindUser {
public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
String Queries[]={"//Employees/Employee/Firstname/City/@value", "//Employees/Employee/Firstname/Lastname/@value"};
for (int x =0; x < Queries.length; x++){
String query = Queries[x];
QueryXML process = new QueryXML(query);
process.query();
}
}
}
これらのクラスは正常に動作しており、コンソールで結果を確認できますが、このプロセスの後にそれを操作するために、「process.query()」の結果を 1 つの変数に割り当てたいと思います。
「for」操作を変数に割り当てて、それをリターン(何か)として返すことが可能かどうか、またはそれが良い考えであるかどうかはわかりません。
どうもありがとう
乾杯!!
ハビ