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
Related
I am trying to unmarshall this XML file :
<OpposantParcelles>
<OpposantParcelle>
<cin>R92107</cin>
<opposant>
<cin>R92107</cin>
<numMarche>xyz1</numMarche>
<verrou xsi:nil="true" />
</opposant>
<num_marche>xyz1</num_marche>
</OpposantParcelle>
<OpposantParcelle>
<cin>R92107</cin>
<opposant>
<cin>R92107</cin>
<numMarche>xyz1</numMarche>
<verrou xsi:nil="true" />
</opposant>
<num_marche>xyz1</num_marche>
</OpposantParcelle>
with the following code :
JAXBContext jaxbContext = JAXBContext.newInstance(OpposantParcelles.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
OpposantParcelles opposantParcelles = (OpposantParcelles) jaxbUnmarshaller.unmarshal( new File("OpposantParcelles.xml"));
System.out.println(opposantParcelles);
But I am getting the following exception stating that the prefix xsi of attribute xsi:nil of element verrou is not bound.
[org.xml.sax.SAXParseException; systemId: file:/D:/Douars/OpposantParcelles.xml; lineNumber: 26; columnNumber: 46; Le préfixe "xsi" de l'attribut "xsi:nil" associé à un type d'élément "verrou" n'est pas lié.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
at ma.ancfcc.webIFE.utils.UnmarshallClass.main(UnmarshallClass.java:102)
Caused by: org.xml.sax.SAXParseException; systemId: file:/D:/Douars/OpposantParcelles.xml; lineNumber: 26; columnNumber: 46; Le préfixe "xsi" de l'attribut "xsi:nil" associé à un type d'élément "verrou" n'est pas lié.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:284)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:308)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
... 6 more
The problem is that I have not included the element verrou in my bean Opposant.
#XmlRootElement(name = "opposant")
#XmlAccessorType (XmlAccessType.FIELD)
public class Opposant {
private String cin;
private String numMarche;
public String getCin() {
return cin;
}
public void setCin(String cin) {
this.cin = cin;
}
public String getNumMarche() {
return numMarche;
}
public void setNumMarche(String numMarche) {
this.numMarche = numMarche;
}
}
Isn't it the case that the SAX parser should ignore this element ? How can I solve this problem ?
You need to intercept deserialisation and provide lost namespace information. It can be done using StreamReaderDelegate class. See below example:
import com.ctc.wstx.sr.InputElementStack;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;
import java.io.File;
import java.io.FileReader;
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(OpposantParcelles.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(xmlFile));
Object unmarshal = jaxbContext.createUnmarshaller().unmarshal(new XsiTypeReader(xsr));
System.out.println(unmarshal);
}
}
class XsiTypeReader extends StreamReaderDelegate {
public XsiTypeReader(XMLStreamReader reader) {
super(reader);
}
#Override
public NamespaceContext getNamespaceContext() {
InputElementStack context = (InputElementStack) super.getNamespaceContext();
context.addNsBinding("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
return context;
}
}
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
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);
I've already tried some approaches but seems to be something on the way I'm doing it.
I created a simple Java class:
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;
#XmlRootElement(name = "TRANSACTION")
#XmlAccessorType(XmlAccessType.FIELD)
public class CanonicalPropertyJaxb {
#XmlAttribute
protected String edbt;
public String getEdbt() {
return edbt;
}
public void setEdbt(String edbt) {
this.edbt = edbt;
}
}
And that's the XML I receive. I need this XML to generate the Java object and the Java object to generate this same XML whenever I need. So I started just trying to get the EDBT property from the TRANSACTION tag, but It's always returning null.
<TRANSACTION ETYPE='JDEMCU' EDUS='IPAAS_ADM' EDBT='16919' EDTN='0' EDLN='2.000' EKEY='IPAAS_ADM-16919-0-2.000'> <TABLE_1 NAME='F0006Z1' TYPE='1'> <FORMAT_TABLE_F0006Z1 SEQ="1"> <COLUMN_EDUS>IPAAS_ADM</COLUMN_EDUS> <COLUMN_EDBT>16919</COLUMN_EDBT> <COLUMN_EDTN>0</COLUMN_EDTN> <COLUMN_EDLN>2.000</COLUMN_EDLN> <COLUMN_EDCT></COLUMN_EDCT> <COLUMN_TYTN>JDEMCU</COLUMN_TYTN> <COLUMN_EDFT></COLUMN_EDFT> <COLUMN_EDDT /> <COLUMN_DRIN>2</COLUMN_DRIN> <COLUMN_EDDL>0</COLUMN_EDDL> <COLUMN_EDSP></COLUMN_EDSP> <COLUMN_PNID></COLUMN_PNID> <COLUMN_TNAC>02</COLUMN_TNAC> <COLUMN_MCU>200</COLUMN_MCU> <COLUMN_STYL>CB</COLUMN_STYL> <COLUMN_DC>FINANCEREPORTTESTBSBU</COLUMN_DC> <COLUMN_LDM>3</COLUMN_LDM> <COLUMN_CO>00200</COLUMN_CO> <COLUMN_AN8>0</COLUMN_AN8> <COLUMN_AN8O>0</COLUMN_AN8O> <COLUMN_CNTY></COLUMN_CNTY> <COLUMN_ADDS></COLUMN_ADDS> <COLUMN_FMOD></COLUMN_FMOD> <COLUMN_DL01>Finance Report Test BS BU</COLUMN_DL01> <COLUMN_DL02></COLUMN_DL02> <COLUMN_DL03></COLUMN_DL03> <COLUMN_DL04></COLUMN_DL04> <COLUMN_RP01>DOM</COLUMN_RP01> <COLUMN_RP02>CAN</COLUMN_RP02> <COLUMN_RP03>NAA</COLUMN_RP03> <COLUMN_RP04>NAA</COLUMN_RP04> <COLUMN_RP05>NAA</COLUMN_RP05> <COLUMN_RP06>ZZ</COLUMN_RP06> <COLUMN_RP07>NAA</COLUMN_RP07> <COLUMN_RP08>NAA</COLUMN_RP08> <COLUMN_RP09>NAA</COLUMN_RP09> <COLUMN_RP10>NAA</COLUMN_RP10> <COLUMN_RP11>NAA</COLUMN_RP11> <COLUMN_RP12>NAA</COLUMN_RP12> <COLUMN_RP13>NAA</COLUMN_RP13> <COLUMN_RP14>NAA</COLUMN_RP14> <COLUMN_RP15>NAA</COLUMN_RP15> <COLUMN_RP16>NAA</COLUMN_RP16> <COLUMN_RP17>NAA</COLUMN_RP17> <COLUMN_RP18>NAA</COLUMN_RP18> <COLUMN_RP19>NAA</COLUMN_RP19> <COLUMN_RP20>NAA</COLUMN_RP20> <COLUMN_RP21>NAA</COLUMN_RP21> <COLUMN_RP22>NAA</COLUMN_RP22> <COLUMN_RP23>NAA</COLUMN_RP23> <COLUMN_RP24></COLUMN_RP24> <COLUMN_RP25></COLUMN_RP25> <COLUMN_RP26></COLUMN_RP26> <COLUMN_RP27></COLUMN_RP27> <COLUMN_RP28></COLUMN_RP28> <COLUMN_RP29></COLUMN_RP29> <COLUMN_RP30></COLUMN_RP30> <COLUMN_TA></COLUMN_TA> <COLUMN_TXJS>0</COLUMN_TXJS> <COLUMN_TXA1></COLUMN_TXA1> <COLUMN_EXR1></COLUMN_EXR1> <COLUMN_TC01></COLUMN_TC01> <COLUMN_TC02></COLUMN_TC02> <COLUMN_TC03></COLUMN_TC03> <COLUMN_TC04></COLUMN_TC04> <COLUMN_TC05></COLUMN_TC05> <COLUMN_TC06></COLUMN_TC06> <COLUMN_TC07></COLUMN_TC07> <COLUMN_TC08></COLUMN_TC08> <COLUMN_TC09></COLUMN_TC09> <COLUMN_TC10></COLUMN_TC10> <COLUMN_ND01></COLUMN_ND01> <COLUMN_ND02></COLUMN_ND02> <COLUMN_ND03></COLUMN_ND03> <COLUMN_ND04></COLUMN_ND04> <COLUMN_ND05></COLUMN_ND05> <COLUMN_ND06></COLUMN_ND06> <COLUMN_ND07></COLUMN_ND07> <COLUMN_ND08></COLUMN_ND08> <COLUMN_ND09></COLUMN_ND09> <COLUMN_ND10></COLUMN_ND10> <COLUMN_CC01>N</COLUMN_CC01> <COLUMN_CC02>N</COLUMN_CC02> <COLUMN_CC03>N</COLUMN_CC03> <COLUMN_CC04>N</COLUMN_CC04> <COLUMN_CC05>N</COLUMN_CC05> <COLUMN_CC06>N</COLUMN_CC06> <COLUMN_CC07>N</COLUMN_CC07> <COLUMN_CC08>N</COLUMN_CC08> <COLUMN_CC09>N</COLUMN_CC09> <COLUMN_CC10>N</COLUMN_CC10> <COLUMN_PECC></COLUMN_PECC> <COLUMN_ALS>6</COLUMN_ALS> <COLUMN_ISS>T</COLUMN_ISS> <COLUMN_GLBA></COLUMN_GLBA> <COLUMN_ALCL>00</COLUMN_ALCL> <COLUMN_LMTH></COLUMN_LMTH> <COLUMN_LF>1.0000</COLUMN_LF> <COLUMN_OBJ1></COLUMN_OBJ1> <COLUMN_OBJ2></COLUMN_OBJ2> <COLUMN_OBJ3></COLUMN_OBJ3> <COLUMN_SUB1></COLUMN_SUB1> <COLUMN_TOU>.00</COLUMN_TOU> <COLUMN_SBLI></COLUMN_SBLI> <COLUMN_ANPA>0</COLUMN_ANPA> <COLUMN_CT>FF</COLUMN_CT> <COLUMN_CERT>N</COLUMN_CERT> <COLUMN_MCUS></COLUMN_MCUS> <COLUMN_BTYP></COLUMN_BTYP> <COLUMN_PC>.00</COLUMN_PC> <COLUMN_PCA>.00</COLUMN_PCA> <COLUMN_PCC>.00</COLUMN_PCC> <COLUMN_INTA></COLUMN_INTA> <COLUMN_INTL></COLUMN_INTL> <COLUMN_D1J /> <COLUMN_D2J /> <COLUMN_D3J /> <COLUMN_D4J /> <COLUMN_D5J /> <COLUMN_D6J /> <COLUMN_FPDJ /> <COLUMN_CAC>.00</COLUMN_CAC> <COLUMN_PAC>.00</COLUMN_PAC> <COLUMN_EEO>N</COLUMN_EEO> <COLUMN_ERC></COLUMN_ERC> <COLUMN_USER>IPAAS_ADM</COLUMN_USER> <COLUMN_PID>ER0006Z1E</COLUMN_PID> <COLUMN_UPMJ>2016/12/15</COLUMN_UPMJ> <COLUMN_JOBN>DJDELOG02</COLUMN_JOBN> <COLUMN_UPMT>144810</COLUMN_UPMT> <COLUMN_URCD></COLUMN_URCD> <COLUMN_URAT>.00</COLUMN_URAT> <COLUMN_URDT /> <COLUMN_URAB>0</COLUMN_URAB> <COLUMN_URRF></COLUMN_URRF> <COLUMN_TORG></COLUMN_TORG> <COLUMN_RP31></COLUMN_RP31> <COLUMN_RP32></COLUMN_RP32> <COLUMN_RP33></COLUMN_RP33> <COLUMN_RP34></COLUMN_RP34> <COLUMN_RP35></COLUMN_RP35> <COLUMN_RP36></COLUMN_RP36> <COLUMN_RP37></COLUMN_RP37> <COLUMN_RP38></COLUMN_RP38> <COLUMN_RP39></COLUMN_RP39> <COLUMN_RP40></COLUMN_RP40> <COLUMN_RP41></COLUMN_RP41> <COLUMN_RP42></COLUMN_RP42> <COLUMN_RP43></COLUMN_RP43> <COLUMN_RP44></COLUMN_RP44> <COLUMN_RP45></COLUMN_RP45> <COLUMN_RP46></COLUMN_RP46> <COLUMN_RP47></COLUMN_RP47> <COLUMN_RP48></COLUMN_RP48> <COLUMN_RP49></COLUMN_RP49> <COLUMN_RP50></COLUMN_RP50> <COLUMN_AN8GCA1>0</COLUMN_AN8GCA1> <COLUMN_AN8GCA2>0</COLUMN_AN8GCA2> <COLUMN_AN8GCA3>0</COLUMN_AN8GCA3> <COLUMN_AN8GCA4>0</COLUMN_AN8GCA4> <COLUMN_AN8GCA5>0</COLUMN_AN8GCA5> </FORMAT_TABLE_F0006Z1> </TABLE_1> </TRANSACTION>
What's the right way to get this information?
JAXB is case sensitive. So either change the field to EDBT, or add the uppercase name to #XmlAttribute(name="EDBT").
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.