Java 从我的XML文件中获取最大Id?

Java 从我的XML文件中获取最大Id?,java,xml,xpath,jaxb,Java,Xml,Xpath,Jaxb,我使用JAXB生成XML,并使用XPATH从XML获取最大id,但从XML获取最大id时出现了问题。异常:“提供的JAXBContext[类com.sun.xml.bind.v2.runtime.JAXBContextImpl]不是EclipseLink JAXBContext,因此无法转换。” 获取最大Id的方法如下: public int GetMaxID() throws JAXBException { try { JAXBContext jc =

我使用JAXB生成
XML
,并使用
XPATH
从XML获取最大id,但从XML获取最大id时出现了问题。异常:“提供的JAXBContext[
类com.sun.xml.bind.v2.runtime.JAXBContextImpl
]不是EclipseLink JAXBContext,因此无法转换。”

获取最大Id的方法如下:

    public int  GetMaxID() throws JAXBException {
      try { 
        JAXBContext jc = JAXBContext.newInstance(Project.class);
        XMLContext xmlContext = JAXBHelper.unwrap(jc, XMLContext.class);///**here is Exception**
        Project project = new Project();
        int maxId =xmlContext.getValueByXPath(project,"Project[not(Project/Layer/@idLayer>@idLayer)]",            null, int.class);
           }catch (Exception e) {
        e.printStackTrace();
           }
         return -1;
     }
我的xml:

<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">//**I want get this Id**
    <LayerName>LayerName</LayerName>
  </Layer>
</Project>

层名称
层名称
层名称
//**我想要这个身份证**
层名称

JAXB将基于XML创建一个项目类

您的JAXB方法有点错误。。。此外,项目类必须与XML内容匹配

JAXBContext jc = JAXBContext.newInstance(Project.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Project xmlData = (Project) unmarshaller.unmarshall(---XML CONTENT---);
那你呢

xmlData.getLayer().get(xmlData.size()-1).getId();
看看这个链接


*编辑您可能会收到错误,因为
Project
与您的XML不匹配(ie没有
层名
属性

谢谢您的回答,但您的代码中没有使用Xpath,请告诉我如何在代码中使用Xpath来获取最大id。我不明白您为什么会同时使用JAXB或Xpath。也许您应该对Xpath进行一些研究