簡體   English   中英

使用JAXB解析GPX

[英]Using JAXB to parse GPX

我正在嘗試使用JAXBU解析GPX文件,這是我的代碼:

GpxType unmarshal(String path) {

    GpxType list = new GpxType();
    try {
        javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext
                  .newInstance(list.getClass().getPackage().getName());
        javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        list = (GpxType) unmarshaller.unmarshal(new java.io.File(path)); //NOI18N
        return list;
    } catch (javax.xml.bind.JAXBException ex) {
        // XXXTODO Handle exception
        java.util.logging.Logger.getLogger("global")
                    .log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
    }
    return null;
}

但是我得到以下錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
                  javax.xml.bind.JAXBElement cannot be cast to GPXfiles.GpxType

所以我猜是因為使用JAXBU是在尋找XML文件而不是GPX文件。 任何幫助,將不勝感激 :)

您可以對unmarshal操作的結果調用JAXBIntrospector.getValue(Object) ,以防止將結果包裝在JAXBElement

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM