2011-03-05 27 views
66
unexpected element (uri:"", local:"Group"). Expected elements are <{}group> 

滿足XMLjavax.xml.bind.UnmarshalException:意想不到元件(URI: 「」,本地: 「組」)

JAXBContext jc = JAXBContext.newInstance(Group.class); 
Unmarshaller unmarshaller = jc.createUnmarshaller(); 
Group group = (User)unmarshaller.unmarshal(new File("group.xml")); 

組類解組異常時沒有任何註釋和組.xml只包含數據。

任何事情都可能是原因?

+0

對於那些來到這裏從搜索,我只是要評論由產生的來源,這是更容易,如果你是混合兩種不同生成的源目錄使用不正確的'ObjectFactory',這是可以引起的。 – bbarker 2017-05-06 01:28:06

回答

79

看起來您的XML文檔具有「組」而不是「組」的根元素「組」。您可以:

  1. 變化你的XML根元素是「組」
  2. 註釋@XmlRootElement(name =「本集團」)添加到組班組長。
+2

它解決了這個問題,謝謝!我使用了第二種解決方案@XmlRootElement(name =「Group」)。我的類名是集團和XML根元素是集團的,爲什麼我還需要NAME =「集團」 – user496949 2011-03-05 11:15:51

+1

退房:http://stackoverflow.com/questions/5203531/what-is-the-default-rule-for- jaxb-unmarshall-the-xml-data/5203585#5203585 – 2011-03-05 11:48:44

+2

@BlaiseDoughan我有完全相反的問題,我有'@XmlRootElement(name =「MB」) @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = 「MB」,propOrder = { 「日期」, 「時刻」})' 但我發現了意想不到'元件(URI:「HTTP:// XX。title.com/new/response「,local:」MB「)。期望的元素是<{}Date>,<{}MB>,<{}Time>'我也有'@XmlElement(name =」Date「,required = true)'在每個字段上。是否出錯?我也嘗試刪除'@ XmlRootElement'! – 2012-05-16 14:25:33

28

您需要將package-info.java放入生成的jaxb包中。它的內容應該是類似的東西

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/StudentOperations/") 
package generated.marsh; 
7

尋找更多之後,根元素必須與一個模式命名空間布萊斯的注意有關。然而,我沒有一個package-info java。因此,在不使用@XMLSchema註解,我能夠通過使用

@XmlRootElement (name="RetrieveMultipleSetsResponse", namespace = XMLCodeTable.NS1) 
@XmlType(name = "ns0", namespace = XMLCodeTable.NS1) 
@XmlAccessorType(XmlAccessType.NONE) 
public class RetrieveMultipleSetsResponse {//...} 

希望這有助於糾正這個問題!

+0

將一個名稱空間添加到根元素rocks!: - ) – 2017-12-29 10:29:48

0

和我一樣。映射類的名稱是Mbean但標籤根的名字是mbean所以我不得不添加註釋:

@XmlRootElement(name="mbean") 
public class MBean { ... } 
11

幸運的是,不需要包信息類。我能用iowatiger08解決方案解決我的問題。

以下是我的修復程序,顯示錯誤消息以幫助加入某些點。

錯誤消息

javax.xml.bind.UnmarshalException:意想不到元件 (URI: 「http://global.aon.bz/schema/cbs/archive/errorresource/0」, 本地: 「errorresource」)。預期元件< {} errorresource>

代碼之前修復

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name="", propOrder={"error"}) 
@XmlRootElement(name="errorresource") 
public class Errorresource 

代碼修復

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name="", propOrder={"error"}) 
@XmlRootElement(name="errorresource", namespace="http://global.aon.bz/schema/cbs/archive/errorresource/0") 
public class Errorresource 

後可以看到的命名空間加入@XmlRootElement在錯誤消息中指示。

1

我有同樣的問題..它幫助我,我指定我的類的相同字段名稱作爲XML文件中的標記名稱(該文件來自外部系統)。

例如:

我的XML文件:

<Response> 
    <ESList> 
    <Item> 
     <ID>1</ID> 
     <Name>Some name 1</Name> 
     <Code>Some code</Code> 
     <Url>Some Url</Url> 
     <RegionList> 
      <Item> 
       <ID>2</ID> 
       <Name>Some name 2</Name> 
      </Item> 
     </RegionList> 
    </Item> 
    </ESList> 
</Response> 

我的迴應類:

@XmlRootElement(name="Response") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Response { 
    @XmlElement 
    private ESList[] ESList = new ESList[1]; // as the tag name in the xml file.. 

    // getter and setter here 
} 

我ESList類:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="ESList") 
public class ESList { 
    @XmlElement 
    private Item[] Item = new Item[1]; // as the tag name in the xml file.. 

    // getters and setters here 
} 

我的項目類:

@XmlRootElement(name="Item") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Item { 
    @XmlElement 
    private String ID; // as the tag name in the xml file.. 
    @XmlElement 
    private String Name; // and so on... 
    @XmlElement 
    private String Code; 
    @XmlElement 
    private String Url; 
    @XmlElement 
    private RegionList[] RegionList = new RegionList[1]; 

    // getters and setters here 
} 

我RegionList類:

@XmlRootElement(name="RegionList") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class RegionList { 
    Item[] Item = new Item[1]; 

    // getters and setters here 
} 

我DemoUnmarshalling類:

public class DemoUnmarshalling { 
    public static void main(String[] args) { 
     try { 
      File file = new File("..."); 

      JAXBContext jaxbContext = JAXBContext.newInstance(Response.class); 
      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
      jaxbUnmarshaller.setEventHandler(
       new ValidationEventHandler() { 
        public boolean handleEvent(ValidationEvent event) { 
         throw new RuntimeException(event.getMessage(), 
          event.getLinkedException()); 
        } 
       } 
      ); 

      Response response = (Response) jaxbUnmarshaller.unmarshal(file); 

      ESList[] esList = response.getESList(); 
      Item[] item = esList[0].getItem(); 
      RegionList[] regionLists = item[0].getRegionList(); 
      Item[] regionListItem = regionLists[0].getItem(); 

      System.out.println(item[0].getID()); 
      System.out.println(item[0].getName()); 
      System.out.println(item[0].getCode()); 
      System.out.println(item[0].getUrl()); 
      System.out.println(regionListItem[0].getID()); 
      System.out.println(regionListItem[0].getName()); 

     } catch (JAXBException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

它提供:

1 
Some name 1 
Some code 
Some Url 
2 
Some name 2 
2

這是一個非常小衆的使用情況下的修復,但它每次都得到我。如果您使用Eclipse Jaxb生成器,它會創建一個名爲package-info的文件。

@javax.xml.bind.annotation.XmlSchema(namespace = "blah.xxx.com/em/feed/v2/CommonFeed") 
package xxx.blah.mh.domain.pl3xx.startstop; 

如果您刪除此文件,將允許更通用的XML來進行解析。試一試!

+1

它在那之後被完美的喚醒了。謝謝你。非常感謝.. :)。我只是提到空字符串的名字空間。 – Arundev 2017-06-13 11:45:25

0

如果上述作品,嘗試添加

@XmlRootElement(name="Group")本集團班組長。

0

對於我來說,原因很簡單,當時想group.xml文件名是在類文件的錯誤混淆改變。看了反編譯的課後,我意識到了這一點。因此,首先檢查引用的文件名是否正確,並在類路徑中可用。

0

我有同樣的問題。 我添加以下屬性<xs:schema..> 將elementFormDefault = 「合格」 attributeFormDefault = 「不合格」

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://www.example.com/schemas/ArrayOfMarketWithStations" 
    targetNamespace="http://www.example.com/schemas/ArrayOfMarketWithStations" 
    elementFormDefault="qualified" attributeFormDefault="unqualified" > 

,並通過運行XJC,其校正package-info.java重新生成的Java類。

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.com/schemas/ArrayOfMarketWithStations", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 

這解決了我的問題。

相關問題