0

我有以下 xml 文档,现在我想用 java 对象中的所有属性值转换 station 元素:

<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

0

假设其余代码正常工作,而不是只返回一个结果,而是返回一个数组或List值......

private static List<String> getAttributValue(String attribute, String xmlResponseBody) {
    List<String> results = new ArrayList<>(25);
    String searchAttributeValue = "";
    try {
    //...//
        try {
        //...//
            NodeList nodeList = (NodeList) result;
            for (int i = 0; i < nodeList.getLength(); i++) {
                results.add(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 results;
}
于 2013-07-11T06:54:36.147 回答