Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
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;
} 


XML
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>
Posted
Updated 9-Apr-14 0:29am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900