Javax.xml.bind.UnmarshalException: unexpected element (uri: ", local:" Group ")

unexpected element (uri:"", local:"Group"). Expected elements are <{}group> 

Meet the exception when unmarshalling from xml

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

The group class has no annotation, and group.xml contains only data.

Can everything be the reason?

+89
java xml jaxb
Mar 05 '11 at 10:50
source share
11 answers

It looks like your XML document has the root element "Group" instead of "group". You can:

  • Change the root element on your XML as a "group"
  • Add the @XmlRootElement (name = "Group") annotation to the group classes.
+105
Mar 05 2018-11-11T00:
source share

You need to put package-info.java in your generated jaxb package. Its content should be something like this.

 @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/StudentOperations/") package generated.marsh; 
+32
Aug 16 '12 at 7:27
source share

Fortunately, the package-info class is not required. I was able to fix the problem with iowatiger08 solution.

Here is my fix showing an error message to help join the dots for some.

Error message

javax.xml.bind.UnmarshalException: unexpected element (uri: " http://global.aon.bz/schema/cbs/archive/errorresource/0 ", local: "errorresource"). Expected Elements: <{} errorresource>

Code before correction

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

Code after correction

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

You can see the namespace added to @XmlRootElement as indicated in the error message.

+25
Aug 28 '13 at 5:18
source share

After looking, moreover, the root element should be associated with the schema namespace, as Blaise notes. However, I did not have a java package. So without using the @XMLSchema annotation, I was able to fix this problem using

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

Hope this helps!

+7
Feb 12 '13 at 22:05
source share

This is a fix for the use case, but it gets me every time. If you use the Eclipse Jaxb generator, it creates a file called package-info.

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

If you delete this file, it will parse more general XML code. Give it a try!

+5
May 11 '16 at 16:07
source share

I had the same problem .. It helped me, I specify the same field names of my classes as the tag names in the XML file (the file comes from an external system).

For example:

My xml file:

 <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> 

My Response Class:

 @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 } 

My ESList class:

 @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 } 

My Item Class:

 @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 } 

My RegionList class:

 @XmlRootElement(name="RegionList") @XmlAccessorType(XmlAccessType.FIELD) public class RegionList { Item[] Item = new Item[1]; // getters and setters here } 

My DemoUnmarshalling class:

 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(); } } } 

He gives:

 1 Some name 1 Some code Some Url 2 Some name 2 
+1
Sep 21 '15 at 11:57
source share

The same goes for me. The mapping class name was Mbean , but the root tag name was Mbean , so I had to add an annotation:

 @XmlRootElement(name="mbean") public class MBean { ... } 
0
Jun 26 '13 at 14:03
source share

For me, the simple reason was that a file name, such as group.xml, was changed if the class file was not properly confused. Looking at the decompiled class, I realized this. Therefore, first check if the file name is specified correctly and is available in the classpath.

0
Oct 11 '17 at 7:38 on
source share

I had the same problem. I added the following attributes to <xs:schema..> elementFormDefault = "qualified" attributeFormDefault = "unqualified"

 <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" > 

and the regenerated java classes by running xjc, which fixed package-info.java.

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

This solved the problem for me.

0
Oct 26 '17 at 2:54 on
source share

You need to put the package-info.java class in the contextPath package and put the code below in the same class:

 @javax.xml.bind.annotation.XmlSchema(namespace = "https://www.namespaceUrl.com/xml/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package com.test.valueobject; 
0
Jun 28 '19 at 5:09
source share

If none of the above work, try adding

@XmlRootElement(name="Group") for group classes.

-one
Nov 14 '16 at 17:37
source share



All Articles