How to marshall XML with namespace in JAXB? - java

I would like to marshal XML using defined XSD schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="myNamespace" xmlns="myNamespace">
<xs:element name="element" type="JavaBean" />
<xs:complexType name="JavaBean">
</xs:complexType>
</xs:schema>
This is empty Java bean type:
package main;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "JavaBean")
public class JavaBean {
}
Simple test results in "Cannot find the declaration of element 'element'." error.
package main;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) throws JAXBException, IOException, SAXException {
final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("sample.xsd"));
JavaBean message = new JavaBean();
JAXBElement<JavaBean> element = new JAXBElement<>(new QName("element"), JavaBean.class, message);
JAXBContext context = JAXBContext.newInstance(JavaBean.class);
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setSchema(schema);
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(element, System.out);
}
}
What is interesting, when I remove targetNamespace and xmlns from XML Schema Definition, it works good.
What am I doing wrong?

According to your schema, the element is bound to the myNamespace namespace by default.
Thus you need to create JavaBean with the proper namespace:
JAXBElement<JavaBean> element = new JAXBElement<>(new QName("myNamespace", "element"), JavaBean.class, message);

Related

XMLDecoder >> java.lang.IllegalArgumentException: Unsupported element

Got this error
java.lang.IllegalArgumentException: Unsupported element: net
from this example xml file
<?xml version="1.0" encoding="UTF-8"?>
<net>
<node label="A">
...
</node>
<node label="B">
...
</node>
<node label="C">
...
</node>
</net>
with these java code lines
...
FileInputStream file = new FileInputStream("example.xml");
XMLDecoder decoder = new XMLDecoder(file);
Object decodedResistors = (Object) decoder.readObject();
file.close();
...
Do not use java.beans.XMLDecoder for deserialisation custom XML payloads. It was not designed for that. Read article Long Term Persistence of JavaBeans Components: XML Schema. It contains some example XML payloads which can be deserialised back by XMLDecoder:
<?xml version="1.0" encoding="UTF-8" ?>
<java version="1.4.0" class="java.beans.XMLDecoder">
<void id="myController" property="owner"/>
<object class="javax.swing.JButton">
<void method="addActionListener">
<object class="java.beans.EventHandler" method="create">
<class>java.awt.event.ActionListener</class>
<object idref="myController"/>
<string>doIt</string>
</object>
</void>
</object>
</java>
If you need to deserialise custom XML use JAXB or Jackson XML. You need to create a POJO model with JAXB annotations:
#XmlRootElement(name = "net")
#XmlAccessorType(XmlAccessType.FIELD)
class Net {
#XmlElement(name = "node")
private List<Node> nodes;
// getters, setters, toString
}
#XmlAccessorType(XmlAccessType.FIELD)
class Node {
#XmlAttribute
private String label;
// getters, setters, toString
}
Example usage:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.File;
import java.util.List;
public class JaxbApp {
public static void main(String[] args) throws Exception {
File xmlFile = new File("./resource/test.xml").getAbsoluteFile();
JAXBContext jaxbContext = JAXBContext.newInstance(Net.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object net = unmarshaller.unmarshal(xmlFile);
System.out.println(net);
}
}
prints:
Net{nodes=[Node{label='A'}, Node{label='B'}, Node{label='C'}]}
See also:
java.lang.IllegalArgumentException: Unsupported element: rss

java copy-of xsl transformation removes namespace prefix

I made a minimal example to reproduce the problem. This is the transformation (mini.xsl):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<foo>
<xsl:copy-of select="."/>
</foo>
</xsl:template>
</xsl:stylesheet>
and this is the input (mini.xml):
<?xml version="1.0" encoding="utf-8"?>
<bar xmlns:x="baz">
<x:baz/>
</bar>
When I apply the transformation with
xsltproc mini.xsl mini.xml
the result looks as expected:
<?xml version="1.0"?>
<foo>
<bar xmlns:x="baz">
<x:baz/>
</bar>
</foo>
However, when I run the transformation with the following Java program,
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class Program
{
public static void main(String[] args)
{
try
{
Source transform = new StreamSource(new FileInputStream(args[0]));
Templates templates = TransformerFactory.newInstance().newTemplates(transform);
Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(args[1]));
Result result = new StreamResult(System.out);
templates.newTransformer().transform(new DOMSource(input), result);
}
catch (TransformerFactoryConfigurationError | ParserConfigurationException | SAXException | IOException | TransformerException e)
{
e.printStackTrace();
}
}
}
the result looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar xmlns:x="baz">
<baz/>
</bar>
</foo>
(notice that the x: prefix in front of baz is missing.)
Why is that?
And what can I do about it (to preserve the namespace prefix)?
For what I can only assume are historical reasons, DocumentBuilderFactory is non-namespace-aware by default. You need to explicitly switch on namespaces before you do newDocumentBuilder().
It would also be better to use the parse method that takes a File directly rather than creating your own FileInputStream (which your code is not closing once the parse is finished), and likewise with the StreamSource from which the Transformer is built.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document input = dbf.newDocumentBuilder().parse(new File(args[1]));

JAXB XmlTransient doesn't work

I'm trying to unmarshall the following XML with JAXB:
<artist xmlns="http://www.spotify.com/ns/music/1">
<name>Basement Jaxx</name>
<albums>
<album href="spotify:album:3xOulZzGRe4Ycwm59iCdKg">
<name>Back 2 the Wild</name>
<artist href="spotify:artist:4YrKBkKSVeqDamzBPWVnSJ">
<name>Basement Jaxx</name>
</artist>
<released>2013</released>
<id type="upc">5055489272702</id>
<album>
</albums>
</artist>
When I use #XmlTransient on the artist within the album to just skip it, it gets parsed regardless.
What can I do to make JAXB handle fields marked #XmlTransient correctly?
Edit 1 - files
Test xml:
<artist>
<name>Adema</name>
<albums>
<album href="spotify:album:07tjJowJIddz8c74x5WOvj">
<name>Topple the Giants</name>
<artist href="spotify:artist:3n4ersmDo55xV4fPSCKpXb">
<name>Adema</name>
</artist>
<released>2013</released>
<id type="upc">886443922082</id>
<availability>
<territories>AD AT AU BE CA CH DE DK EE ES FI FR GB HK IE IS IT LI LT LU LV MC MX MY NL NO NZ PL PT SE SG US</territories>
</availability>
</album>
<album href="spotify:album:06QaJLqG068uXHQbAcPZKU">
<name>Kill The Headlights</name>
<artist href="spotify:artist:3n4ersmDo55xV4fPSCKpXb">
<name>Adema</name>
</artist>
<released>2007</released>
<id type="upc">4030816195283</id>
<availability>
<territories>AD AT BE CH DE DK EE ES FI FR GB IE IS IT LI LT LU LV MC NL NO PL PT SE</territories>
</availability>
</album>
</albums>
</artist>
XTest.java:
package dao.spotify;
import dao.spotify.lookup.entities.LookupArtist;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.helpers.DefaultValidationEventHandler;
public class XTest {
public XTest() {
load();
}
public void load() {
try {
File file = new File("c:/tmp/test5.xml");
JAXBContext jc = JAXBContext.newInstance(LookupArtist.class);
Unmarshaller um = jc.createUnmarshaller();
um.setEventHandler(new DefaultValidationEventHandler());
LookupArtist spotifyArtistWrapper = (LookupArtist) um.unmarshal(file);
System.err.println("Done");
} catch (JAXBException ex) {
ex.printStackTrace();
}
}
public static void main(String... args) {
XTest test = new XTest();
}
}
LookupArtist.java:
package dao.spotify.lookup.entities;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "artist")
#XmlAccessorType(XmlAccessType.FIELD)
public class LookupArtist {
#XmlElement
private String name;
#XmlElementWrapper(name = "albums")
#XmlElement(name = "album")
private List<LookupInnerAlbum> albums = new ArrayList();
}
LookupInnerAlbum.java:
package dao.spotify.lookup.entities;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
#XmlRootElement(name = "album")
#XmlAccessorType(XmlAccessType.FIELD)
public class LookupInnerAlbum {
#XmlAttribute
private String href;
private String name;
private int released;
private String id;
private LookupInnerAvailability availability;
#XmlTransient
private LookupInnerArtist artist;
}
LookupInnerAvailability.java:
package dao.spotify.lookup.entities;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "availability")
#XmlAccessorType(XmlAccessType.FIELD)
public class LookupInnerAvailability {
#XmlElement
private String territories;
}
LookupInnerArtist.java:
package dao.spotify.lookup.entities;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
#XmlType(name = "artist")
#XmlAccessorType(XmlAccessType.FIELD)
public class LookupInnerArtist {
#XmlElement
private String name;
#XmlAttribute
private String href;
}
When running this I get:
DefaultValidationEventHandler: [ERROR]: unexpected element (uri:"", local:"artist"). Expected elements are <{}id>,<{}released>,<{}name>,<{}availability>
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"artist"). Expected elements are <{}id>,<{}released>,<{}name>,<{}availability>
Location: line 6 of file:/c:/tmp/test5.xml
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:105)
at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:262)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2717)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:136)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:141)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:150)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:168)
at dao.spotify.XTest.load(XTest.java:30)
at dao.spotify.XTest.<init>(XTest.java:21)
at dao.spotify.XTest.main(XTest.java:38)
When I run your example I get the same thing. The error message is correct unexpected element (uri:"", local:"artist") the element exists in the XML but you have not mapped to it. The expected elements you have mapped to are <{}id>,<{}released>,<{}name>,<{}availability> the element <{}artist> does not appear there because you have excluded it with #XmlTransient.
DefaultValidationEventHandler: [ERROR]: unexpected element (uri:"", local:"artist"). Expected elements are <{}id>,<{}released>,<{}name>,<{}availability>
Location: line 6 of file:/Users/bdoughan/GIT/EclipseLink-Trunk3/Scratch/src/dao/spotify/test5.xml
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"artist"). Expected elements are <{}id>,<{}released>,<{}name>,<{}availability>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
Note about DefaultValidationEventHandler. This class represents the default validation event rules for JAXB 1.0 which are different from JAXB 2.0. The following is from the Javadoc (see: http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/helpers/DefaultValidationEventHandler.html)
JAXB 1.0 only default validation event handler. This is the default
handler for all objects created from a JAXBContext that is managing
schema-derived code generated by a JAXB 1.0 binding compiler.
This handler causes the unmarshal and validate operations to fail on
the first error or fatal error.
This handler is not the default handler for JAXB mapped classes
following JAXB 2.0 or later versions. Default validation event
handling has changed and is specified in Unmarshaller and Marshaller.
If you had not specified this as the ValidationEventHandler then your document would have unmarshalled correctly.
UPDATE
I'm afraid I don't understand; I thought that I had mapped artist when
specifying: private LookupInnerArtist artist;? When changing the
DefaultValidationEventHandler to ValidationEventCollector (correct?)
everything works fine, even if I have specified artist as
#XmlTransient.
Since you have specified #XmlAccessorType(XmlAccessType.FIELD) all fields except those annotated with #XmlTransient will be considered mapped.
I can still see artist when debugging, though it has a null value.
Even though artist is annotated with #XmlTransient it still remains available on your class. Since it is now considered unmapped it will not be populated by the unmarshal operation.
For More Information
http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html

Create JAXB Object from large XML file

I'm looking for something to easily create an Object to access a large XML file.
The XML file looks like this:
<?xml version="1.0" encoding="WINDOWS-1252"?>
<vzg:vzg erstellt_von="##" erstellt_am="###" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vzg="###" xsi:schemaLocation="###">
<auswahl sicht="B" basisfplp_id="0" basisve_id="0">
<fplp vzg_id="0" periode="2012/2013"/>
<version vzg_id="###" name="###"/>
<strecke name="11801">
<von baukms_nr="###" km="#.#"/>
<bis baukms_nr="###" km="#.#"/>
</strecke>
<bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
<bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
<bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
<bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
<bst vzg_id="#" name="#" kurzbez="##" bez="####" kritart="#"/>
...
I want an Object to calculate with some of the XML Attributes.
Like:
List vzg_id=vzg.auswahl.bst;
int res=vzg_id.get(3) * vzg.auswahl.strecke.von.baukms_nr;
Since the XML has about 16000 Lines it's difficult to create a class for every XMLElement.
What I've done now:
MainClass
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.File;
import javax.xml.bind.JAXB;
public class Main
{
public static void main(String[] args)
{
VZG vzg = JAXB.unmarshal(new File("./XMLVZG.xml"), VZG.class);
System.out.println(vzg.erstellt_am+ " "+vzg.erstellt_von+"\n"+vzg.aw.sicht);
}
}
Class VZG
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class VZG
{
#XmlElement(name="auswahl")
AuswahlSicht aw;
#XmlAttribute(name="erstellt_von")
String erstellt_von;
#XmlAttribute(name="erstellt_am")
String erstellt_am;
#XmlAttribute(name="xsi")
String xmlns_xsi;
}
Class Auswahl
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="auswahl")
public class AuswahlSicht
{
#XmlAttribute(name="basisfplp_id")
int basisfplp_id;
#XmlAttribute(name="basisve_id")
int basisve_id;
#XmlAttribute(name="sicht")
String sicht;
}
So I'm now able to get the Attributes of the Root and the Cild, but I have still about 1000 childs with Attributes left and I'm looking for an automated way to parse the XML to get an object.
Simple Description:
XML File
<root>
<child>
<Subchild id="1"/>
<subsubchild id=2/>
<subsubchild id=33/>
</child>
</root>
The object should then be like this:
List subsubchilds = root.child.subchild.subsubchild;
int id_one=subsubchilds.get(0);
Thanks in advance
It is rarely a good idea to write JAXB classes by hand for existing XML. JDK has special command line tool to generate these classes for you (xjc) from XML schema. If schema is not available you could try to generate schema from XML (various tools can do that - for example XMLSpy) and then generate classes using xjc.

how to find in xsd, whether the particular tag is available in xml or not?

My xml file is,
<?xml version="1.0"?>
<type xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:noNamespaceSchemaLocation="datatype.xsd">
<int>integer</int>
<varchar>varcharacter</varchar>
<double>doubles</double>
</type>
In this xml, I want to set <float></float> as mandatory. But i didn't use this tag. So how to validate the <float> is present or not in my xml file, using xsd with java.? Thanks in advance.
The following can be used to validate your XML against a schema:
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
public class Demo {
public static void main(String[] args) throws Exception {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("datatype.xsd"));
Validator validator = schema.newValidator();
validator.setErrorHandler(new MyErrorHandler());
validator.validate(source);
}
}
For a more detailed example see:
http://bdoughan.blogspot.com/2010/11/validate-jaxb-object-model-with-xml.html

Categories