Javax.Xml.Bind.Unmarshalexception: Unexpected Element (Uri:"", Local:"Group")

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

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

  1. Change the root element on your XML to be "group"
  2. Add the annotation @XmlRootElement(name="Group") to the Group classs.

How to fix: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"catalog"). Expected elements are (none)

I've checked and can confirm. You need to

  • add @XmlRootElement(name = "catalog") on your Catalog class to tell JAXB this can be at the root
  • change annotation to @XmlElement(name="book") on getBooks() method (otherwise doesn't match, look for books)
  • add annotation @XmlAttribute on Book setId method (otherwise you end up with empty ids)

And then your code works:

Books:
bk101 Gambardella, Matthew XML Developer's Guide
bk102 Ralls, Kim Midnight Rain
...

Error when unexpected element (uri:"", local:"response"). Expected elements are <{}quote>

You JAXB context is created only with the Quote class:

JAXBContext.newInstance(Quote.class);

So JAXB does not know about the Response class which configures the top-level response element.

One of the options is to add Response to the enumeration of classes you create JAXBContext for:

JAXBContext.newInstance(Response.class, Quote.class)

Alternatively, create JAXBContext for the whole package:

JAXBContext.newInstance(Quote.class.getPackage().getName())

I prefer the second option.

javax.xml.bind.UnmarshalException: unexpected element (uri:"MyProtocol.xsd", local:"MyFrame"). Expected elements are (none)

Never mind, I figured it out. If you have this XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<MyFrame>
<Data>
<ID>172</ID>
<Name>101</Name>
<Date>11241987</Date>
</Data>
</MyFrame>

You can unmarshal it with the below code. The code in the OP only works if using JAXB. However, in my scenario, I was using ADB.

Unmarshal

XMLStreamReader reader = XMLInputFactory.newInstance()
.createXMLStreamReader(new ByteArrayInputStream(someXMLString.getBytes()));

SomeClass myClass = SomeClass.Factory.parse(reader);

Marshal (If you want to get the above XML from a class):

OMElement omElement = myClass.getOMElement
(SomeClass.MY_QNAME, OMAbstractFactory.getSOAP12Factory());
String someXMLString = omElement.toStringWithConsume();


Related Topics



Leave a reply



Submit