如何通过xquery从xml获取max id [英] how get max id from xml by xquery

查看:117
本文介绍了如何通过xquery从xml获取max id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题我需要从我的XML获取最大ID的最佳方法。我尝试通过Xpath获取最大ID但这段代码不起作用。请问你能给我一个例子如何从我的XML文件中获取max id。

我在java中的方法:

I have problem I need best way for get max id from my XML.I try get max id by Xpath but this code not work. please can you give me any example how can get max id from my XML file in java.
my Method in java :

public int GetMaxID() throws JAXBException {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        String expression = "//Project/Layer/@idLayer[not(. <=../preceding-sibling::Layer/@idLayer) and not(. <=../following-sibling::Layer/@idLayer)]";
        XPathExpression xPathExpression = xPath.compile(expression);
        InputSource doc = newInputSource(newInputStreamReader(newFileInputStream(newFile("Projects//asdad//ProjectDataBase.xml"))));
        NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
        int maxId = elem1List.getLength();//give me 0
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
} 







My XML code:

<Project  name="p1"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <Layer idLayer="0">
        <LayerName>LayerName</LayerName>
    </Layer>
    <Layer idLayer="1">
        <LayerName>LayerName</LayerName>
    </Layer>
    <Layer idLayer="2">
        <LayerName>LayerName</LayerName>
    </Layer>
    <Layer idLayer="3">//that is max id
        <LayerName>LayerName</LayerName>
    </Layer>
</Project>

推荐答案

这篇关于如何通过xquery从xml获取max id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆