1

Java で XML ファイルを解析するのに問題があります。ファイルの形式は次のとおりです。

<root>
  <thing>
    <name>Thing1</name>
    <property>
      <name>Property1</name>
    </property>
    ...
  </thing>
  ...
</root>

最終的に、このファイルを Thing オブジェクトのリストに変換したいと思います。このリストには String 名 (Thing1) があり、Property オブジェクトのリストにも名前 (Property1) があります。

このデータを取得するために xpaths を使用しようとしてきましたが、「もの」の名前だけを取得しようとすると、「プロパティ」のものを含め、「もの」に表示されるすべての名前が得られます。私のコードは次のとおりです。

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(filename);
XPath xpath = XPathFactory.newInstance().newXPath();


XPathExpression thingExpr = xpath.compile("//thing");
NodeList things = (NodeList)thingExpr.evaluate(dom, XPathConstants.NODESET);
for(int count = 0; count < things.getLength(); count++)
{
    Element thing = (Element)things.item(count);
    XPathExpression nameExpr = xpath.compile(".//name/text()");
    NodeList name = (NodeList) nameExpr.evaluate(thing, XPathConstants.NODESET);
    for(int i = 0; i < name.getLength(); i++)
    {
        System.out.println(name.item(i).getNodeValue());    
    }
}

誰でも助けることができますか?前もって感謝します!

4

3 に答える 3

1

次のようなものを試すことができます...

public class TestXPath {

    public static void main(String[] args) {
        String xml =
                        "<root>\n"
                        + "    <thing>\n"
                        + "        <name>Thing1</name>\n"
                        + "        <property>\n"
                        + "            <name>Property1</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property2</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property3</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property4</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property5</name>\n"
                        + "        </property>\n"
                        + "    </thing>/n"
                        + "    <NoAThin>\n"
                        + "        <name>Thing2</name>\n"
                        + "        <property>\n"
                        + "            <name>Property1</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property2</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property3</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property4</name>\n"
                        + "        </property>\n"
                        + "        <property>\n"
                        + "            <name>Property5</name>\n"
                        + "        </property>\n"
                        + "    </NoAThin>/n"
                        + "</root>";

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
            Document dom = db.parse(bais);
            XPath xpath = XPathFactory.newInstance().newXPath();

            // Find the "thing" node...
            XPathExpression thingExpr = xpath.compile("/root/thing");
            NodeList things = (NodeList) thingExpr.evaluate(dom, XPathConstants.NODESET);

            System.out.println("Found " + things.getLength() + " thing nodes...");

            // Find the property nodes of thing
            XPathExpression expr = xpath.compile("property");
            NodeList nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);

            System.out.println("Found " + nodes.getLength() + " thing/property nodes...");

            // Find all the property "name" nodes under thing
            expr = xpath.compile("property/name");
            nodes = (NodeList) expr.evaluate(things.item(0), XPathConstants.NODESET);

            System.out.println("Found " + nodes.getLength() + " name nodes...");
            System.out.println("Property value = " + nodes.item(0).getTextContent());

            // Find all nodes that have property nodes
            XPathExpression exprAll = xpath.compile("/root/*/property");
            NodeList nodesAll = (NodeList) exprAll.evaluate(dom, XPathConstants.NODESET);
            System.out.println("Found " + nodesAll.getLength() + " property nodes...");

        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}

次のような出力が得られます

Found 1 thing nodes...
Found 5 thing/property nodes...
Found 5 name nodes...
Property value = Property1
Found 10 property nodes...
于 2012-10-19T05:26:36.943 に答える
0

どう"//thing/name/text()"ですか?

以前の二重スラッシュはname、「ツリー内の任意の場所であり、必ずしも直接の子ノードではない」ことを意味します。

于 2012-10-19T01:54:55.450 に答える
0

次の XPath 式を使用します

//thing[name='Thing1']

これにより、文字列値が でthingある子を持つ XML ドキュメント内の任意の要素が選択されます。name"Thing1"

も使用します

//property[name='Property1']

これにより、文字列値が でpropertyある子を持つ XML ドキュメント内の任意の要素が選択されます。name"Property1"

更新

thingそれぞれが要素の文字列値を含むすべてのテキスト ノードを取得するには、次のようにします。

//thing/text()

XPath 2.0 では、以下を使用して文字列自体のシーケンスを取得できます。

//thing/string(.)

これは単一の XPath 式では不可能ですが、次のthingように特定の (n 番目の) 要素の文字列値を取得できます。

string((//thing)[$n])

ここで$n、1 から までの特定の数値に置き換える必要がありますcount(//thing)

したがって、プログラミング言語では、まず次のcntXPath 式を評価して判断できます。

count(//thing)

そして、for $nfrom 1のループでcntxpath 式を動的に生成して評価します。

string((//thing)[$n])

property要素のすべての値を取得する場合もまったく同じです。

于 2012-10-19T01:56:08.340 に答える