0

次の xml ドキュメントがあり、ステーション要素を Java オブジェクトのすべての属性値で変換したいと考えています。

<LocValRes flag="FINAL" id="dummy">
    <Station name="Sampieri" distance="2901857" duration="3482348" externalId="008302686#80" externalStationNr="008302686" type="WGS84" x="14744601" y="36731901"/>
    <Station name="Pozzallo" distance="2903750" duration="3484620" externalId="008302642#80" externalStationNr="008302642" type="WGS84" x="14847168" y="36733609"/>
    <Station name="Scicli" distance="2907477" duration="3489092" externalId="008302695#80" externalStationNr="008302695" type="WGS84" x="14699187" y="36790088"/>
    <Station name="Ispica" distance="2909739" duration="3491806" externalId="008302562#80" externalStationNr="008302562" type="WGS84" x="14914362" y="36778043"/>
    <Station name="Rosolini" distance="2914534" duration="3497560" externalId="008302660#80" externalStationNr="008302660" type="WGS84" x="14952836" y="36815950"/>
    <Station name="Modica" distance="2915249" duration="3498418" externalId="008300462#80" externalStationNr="008300462" type="WGS84" x="14754166" y="36852950"/>
    <Station name="Donnafugata" distance="2915468" duration="3498681" externalId="008302530#80" externalStationNr="008302530" type="WGS84" x="14568817" y="36882183"/>
    <Station name="Ragusa" distance="2922026" duration="3506551" externalId="008300336#80" externalStationNr="008300336" type="WGS84" x="14725427" y="36919209"/>
    <Station name="Comiso" distance="2923096" duration="3507835" externalId="008302524#80" externalStationNr="008302524" type="WGS84" x="14600540" y="36947283"/>
    <Station name="Vittoria" distance="2923168" duration="3507921" externalId="008300491#80" externalStationNr="008300491" type="WGS84" x="14524959" y="36958762"/>
    <Station name="Noto" distance="2923895" duration="3508794" externalId="008302609#80" externalStationNr="008302609" type="WGS84" x="15074307" y="36882498"/>
    <Station name="Acate" distance="2925789" duration="3511066" externalId="008302445#80" externalStationNr="008302445" type="WGS84" x="14425430" y="36996652"/>
    <Station name="Avola" distance="2927532" duration="3513158" externalId="008302461#80" externalStationNr="008302461" type="WGS84" x="15125762" y="36907586"/>
    <Station name="Gela-Anic" distance="2929204" duration="3515164" externalId="008302743#80" externalStationNr="008302743" type="WGS84" x="14299176" y="37045013"/>
    <Station name="Licata" distance="2930810" duration="3517092" externalId="008300380#80" externalStationNr="008300380" type="WGS84" x="13938278" y="37105987"/>
    <Station name="Gela" distance="2931688" duration="3518145" externalId="008300310#80" externalStationNr="008300310" type="WGS84" x="14259255" y="37072988"/>
    <Station name="Falconara" distance="2933198" duration="3519957" externalId="008302532#80" externalStationNr="008302532" type="WGS84" x="14052468" y="37113511"/>
    <Station name="Niscemi" distance="2941720" duration="3530184" externalId="008300423#80" externalStationNr="008300423" type="WGS84" x="14403505" y="37144992"/>
    <Station name="Siracusa" distance="2947889" duration="3537586" externalId="008300338#80" externalStationNr="008300338" type="WGS84" x="15281733" y="37068907"/>
    <Station name="Campobello Ravanusa" distance="2948468" duration="3538281" externalId="008302485#80" externalStationNr="008302485" type="WGS84" x="13946350" y="37265582"/>
</LocValRes>

つまり、駅の xml 要素ごとに独自の Java オブジェクトが必要です。それは問題ありません。このタスクの解決策があります。ここで、XPath を使用して各属性値を抽出したいと考えています。次の Java コードを記述しました。

 private static String getAttributValue(String attribute, String xmlResponseBody) {

    String searchAttributeValue = "";
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new InputSource(new StringReader(xmlResponseBody)));
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        try {
            XPathExpression expr = xpath.compile("//@" + attribute);
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodeList = (NodeList) result;
            for (int i = 0; i < nodeList.getLength(); i++) {
                searchAttributeValue = nodeList.item(i).getTextContent();
            }
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (SAXException sae) {
        sae.printStackTrace();
    }
    return searchAttributeValue;
}

しかし、ループ...

for (int i = 0; i < nodeList.getLength(); i++) {
            searchAttributeValue = nodeList.item(i).getTextContent();
        }

... 最後のステーション要素から値のみを抽出します。XPath式に位置述語が必要だと思います。おそらくこれと非常に似ています:

//@" + attribute + [position() + i +]

この Java For Loop コードで利用可能な各ステーション要素の各属性値を抽出するにはどうすればよいですか?

4

1 に答える 1