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>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)