Use JAXB to create Object from XML String

To pass XML content, you need to wrap the content in a Reader, and unmarshal that instead: JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = new StringReader(“xml string here”); Person person = (Person) unmarshaller.unmarshal(reader);

Using StAX to create index for XML for quick access

You could work with a generated XML parser using ANTLR4. The Following works very well on a ~17GB Wikipedia dump /20170501/dewiki-20170501-pages-articles-multistream.xml.bz2 but I had to increase heap size using -xX6GB. 1. Get XML Grammar cd /tmp git clone https://github.com/antlr/grammars-v4 2. Generate Parser cd /tmp/grammars-v4/xml/ mvn clean install 3. Copy Generated Java files to your Project … Read more

How to deal with JAXB ComplexType with MixedContent data?

As you mentioned you need to add the mixed attribute to indicate that your type supports mixed content. Without this specified your XML content is invalid: <xsd:complexType name=”TaxDescriptionType” mixed=”true”> <xsd:sequence> <xsd:element name=”ShortName” type=”xsd:string” /> </xsd:sequence> <xsd:attribute ref=”xml:lang” /> </xsd:complexType> The generated TaxDescriptionType class will have the following property. Essentially this means that all of the … Read more

JAXB filtered parsing

You could use the @XmlPath extension in EclipseLink JAXB (MOXy) to handle this case (I’m the MOXy tech lead): @XmlRootElement(name=”addressbook”) public class Addressbook implements Serializable { private ArrayList<Company> companyList = new ArrayList<Company>(); public Addressbook() { } @XmlPath(“company[@name=”abc”]”) public ArrayList<Company> getCompanyList() { return companyList; } } For More Information: http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html UPDATE – Using StreamFilter The example … Read more

JAXB unmarshall with namespaces and prefix

This can be done without modifying the generated JAXB code using standard SOAPMessage class. I wrote about this here and here It’s a little fiddly but works correctly. Marshalling Farm farm = new Farm(); farm.getHorse().add(new Horse()); farm.getHorse().get(0).setName(“glue factory”); farm.getHorse().get(0).setHeight(BigInteger.valueOf(123)); Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Marshaller marshaller = JAXBContext.newInstance(Farm.class).createMarshaller(); marshaller.marshal(farm, document); SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); soapMessage.getSOAPBody().addDocument(document); ByteArrayOutputStream … Read more

How generate XMLElementWrapper annotation with xjc and customized binding

Bjarne Hansen developed a plugin for xjc that was able to take care of this. Unfortunately, the link to the original implementation is now dead. However, there is a project by Dmitry Katsubo on github, based on Bjarne’s original code with some additional improvements. → https://github.com/dmak/jaxb-xew-plugin (Just for reference: the original link, now dead: http://www.conspicio.dk/blog/bjarne/jaxb-xmlelementwrapper-plugin)

Dynamic tag names with JAXB

You can use an @XmlAnyElement-annotated property and return the elements as JAXBElements: private Map<String, Integer> months = …; @XmlAnyElement public List<JAXBElement<Integer>> getMonths() { List<JAXBElement<Integer>> elements = new ArrayList<JAXBElement<Integer>>(); for (Map.Entry<String, Integer> month: months.entrySet()) elements.add(new JAXBElement(new QName(month.getKey()), Integer.class, month.getValue())); return elements; } This approach is ugly, but not uglier than the XML it produces.

Java/JAXB: Unmarshall XML attributes to specific Java object attributes

How about? Introduce a common super class called Options: import javax.xml.bind.annotation.XmlAttribute; public abstract class Options { private String name; @XmlAttribute public String getName() { return name; } public void setName(String name) { this.name = name; } } Then on your class with the list of options (Configuration in this example), specify an @XmlJavaTypeAdapter on that … Read more