Skip to content

Instantly share code, notes, and snippets.

@dalelane
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalelane/88df784c3cb74b214d5c to your computer and use it in GitHub Desktop.
Save dalelane/88df784c3cb74b214d5c to your computer and use it in GitHub Desktop.
JAXB problem... a simple re-create of a problem I'm having with unmarshalling using JAXB. Fixed working version is at https://gist.github.com/dalelane/983f278d2485d29a6ef4
package sandbox;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class JaxbProblem {
//----------------------------------------
// Output from running this sample
// when unmarshalling XML_WITH_SPACES it works - and unmarshalls all five items
// when unmarshalling XML_WITHOUT_SPACES it loses every other item
//----------------------------------------
// About to process a new XML document...
// >one
// >two
// >three
// >four
// >five
// About to process a new XML document...
// >one
// >three
// >five
// five items in XML fragments
private static final String TEST1_STR = "<test key=\"one\"></test>";
private static final String TEST2_STR = "<test key=\"two\"></test>";
private static final String TEST3_STR = "<test key=\"three\"></test>";
private static final String TEST4_STR = "<test key=\"four\"></test>";
private static final String TEST5_STR = "<test key=\"five\"></test>";
// combine the five items into XML documents - with and without whitespace in between
private static final String XML_WITH_SPACES =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<jaxbissue>" +
TEST1_STR + " " + TEST2_STR + " " + TEST3_STR + " " + TEST4_STR + " " + TEST5_STR + " " +
"</jaxbissue>";
private static final String XML_WITHOUT_SPACES =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<jaxbissue>" +
TEST1_STR + TEST2_STR + TEST3_STR + TEST4_STR + TEST5_STR +
"</jaxbissue>";
public static void main(String[] args)
{
String[] tests = { XML_WITH_SPACES, XML_WITHOUT_SPACES };
for (String test : tests){
System.out.println("About to process a new XML document...");
try {
// prepare a stream to read the XML document
InputStream stream = new ByteArrayInputStream(test.getBytes());
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(stream);
// prepare an unmarshaller to turn the XML into objects
JAXBContext context = JAXBContext.newInstance(Test.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
boolean running = true;
while (running){
switch (reader.next()){
case XMLStreamConstants.START_ELEMENT:
if (reader.getLocalName().equals(Test.XMLELEMENTNAME)){
JAXBElement<Test> unmarshalledObj = unmarshaller.unmarshal(reader, Test.class);
Test item = unmarshalledObj.getValue();
System.out.println(">" + item.getKey());
}
break;
case XMLStreamConstants.END_DOCUMENT:
reader.close();
running = false;
break;
}
}
}
catch (Exception exc){
exc.printStackTrace();
}
}
}
@XmlRootElement(name=Test.XMLELEMENTNAME)
public static class Test {
public static final String XMLELEMENTNAME = "test";
protected String key;
@XmlAttribute
public String getKey() {
return key;
}
public void setKey(String value) {
this.key = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment