Igor's Blog

I've recently had a requirement to unmarshal an XML fragment that was passed into one of my services. This in itself is typically not and issue and I've written code that does that plenty a time, however in this case I was having to unmarshal a fragment for a complex type, not for an element. That's where it got a little bit more complicated.

Initially my code was throwing the following exception:
 Exception
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://net.igorkromin/", local:"MyComplexType"). Expected elements are (none)


So that meant that the unmarshaller didn't recognise any valid elements in my XML string that I was trying to unmarshal. I checked the JAXB Context and it definitely had the complex type I was after so it was something else I was doing wrong.

Lets take a step back and look at the schema, my complex type, MyComplexType, was defined as:
 XSD
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://net.igorkromin/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://net.igorkromin/" elementFormDefault="qualified">
...
<xsd:complexType name="MyComplexType">
<xsd:sequence>
...
</xsd:sequence>
</xsd:complexType>
...


That resulted in a JAXB generated class that looked like:
 Java Class
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "MyComplexType",
propOrder = {...}
)
public class MyComplexType {
...
}




So, no XmlRootElement annotation. That's because it was a complex type and not an element of that type. That should have been obvious right away, but I missed it initially. Luckily there was a simple way around it. All I had to do was tell JAXB what complex type I was expecting and wrap the XML fragment as a Source or specifically StreamSource.

So the code that would achieve that looked something like...
 Java
String xmlString =
"< MyComplexType xmlns=\"http://net.igorkromin/\">\n" +
" ..." +
"</MyComplexType >";
JAXBContext ctx = JAXBContext.newInstance(MyComplexType.class);
Unmarshaller u = ctx.createUnmarshaller();
ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes());
Source source = new StreamSource(bais);
JAXBElement<MyComplexType> mct = u.unmarshal(source, MyComplexType.class);


That worked like a charm and I could then get my unmarshalled complex type instance from the JAXBElement instance.

-i

comments powered by Disqus
Other posts you may like...
Programming, DIY, Games, Hacks, and Tech

Recent Blog Posts

Swapping HDD logic boards between different brand disks - is it possible?

A guide for plotting your hard drive for Burstcoin mining on a Mac using mjminer

Replacing a broken hard drive in a Samsung Story Station

Fix Java applications slow start and net connection times on macOS sierra

Pierre Cardin Leather Computer Bag (PC2278)

How to stop SSH remote host disconnecting your session

Using PayPal PHP SDK on Google App Engine

Use the Touch Bar to lock your Mac with a single button press

Multi module builds with Maven 3.5 and the parent-child pom version management

Google App Engine local dev server DataStore becoming corrupted after a bad GQL statement is run

Recent Galleries

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Don't feel obliged, but you could follow me for more great content!
     
Don't show this again