0

私がやりたかったのは、次のクラスから生成された結果を取得することです:

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」操作を変数に割り当てて、それをリターン(何か)として返すことが可能かどうか、またはそれが良い考えであるかどうかはわかりません。

どうもありがとう

乾杯!!

ハビ

4

3 に答える 3

1

これらのクラスは正常に動作しており、コンソールで結果を確認できますが、このプロセスの後にそれを操作するために、「process.query()」の結果を 1 つの変数に割り当てたいと思います。

したがって、関数 vom "void" の戻り値の型を fe "org.w3c.dom.Document" に変更する必要があり、有効な xml ドキュメントを返すように関数を変更する必要があります。

于 2013-10-16T13:35:19.930 に答える
1

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());
        }
  }
于 2013-10-16T13:45:06.757 に答える
0

あなた のタグがこのようなものである場合System.out.print(nodes.item(i).getNodeValue());System.out.print(nodes.item(0).getFirstChild().getNodeValue());、null もチェックする必要があります。getFirstChild<a/>

于 2013-10-16T13:35:15.553 に答える