How to remove #XmlElementRefs and replace them with java properties - jaxb

I have the following class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"content"
})
#XmlRootElement(name = "SegmentFilter")
public class SegmentFilter {
#XmlElementRefs({
#XmlElementRef(name = "StopRange", type = JAXBElement.class),
#XmlElementRef(name = "OverlapRange", type = JAXBElement.class),
#XmlElementRef(name = "DateRange", type = JAXBElement.class),
#XmlElementRef(name = "DurationRange", type = JAXBElement.class),
#XmlElementRef(name = "EmployeeSelector", type = JAXBElement.class),
#XmlElementRef(name = "SegmentCode", type = JAXBElement.class),
#XmlElementRef(name = "StartRange", type = JAXBElement.class)
})
#XmlMixed
protected List<Serializable> content;
#XmlAttribute(name = "IncludeDetailSegments")
protected Boolean includeDetailSegments;
#XmlAttribute(name = "IncludeGeneralSegments")
protected Boolean includeGeneralSegments;
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
public boolean isIncludeDetailSegments() {
if (includeDetailSegments == null) {
return true;
} else {
return includeDetailSegments;
}
}
public void setIncludeDetailSegments(Boolean value) {
this.includeDetailSegments = value;
}
public boolean isIncludeGeneralSegments() {
if (includeGeneralSegments == null) {
return true;
} else {
return includeGeneralSegments;
}
}
public void setIncludeGeneralSegments(Boolean value) {
this.includeGeneralSegments = value;
}
}
Can someone please help me replace the ref with actual java code like ...
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"content"
})
#XmlRootElement(name = "SegmentFilter")
public class SegmentFilter {
public DateTimeRangeType StopRange;
public DateTimeRangeType StartRange;
//and so on with getters and setters
#XmlAttribute(name = "IncludeDetailSegments")
protected Boolean includeDetailSegments;
#XmlAttribute(name = "IncludeGeneralSegments")
protected Boolean includeGeneralSegments;
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
public boolean isIncludeDetailSegments() {
if (includeDetailSegments == null) {
return true;
} else {
return includeDetailSegments;
}
}
public void setIncludeDetailSegments(Boolean value) {
this.includeDetailSegments = value;
}
public boolean isIncludeGeneralSegments() {
if (includeGeneralSegments == null) {
return true;
} else {
return includeGeneralSegments;
}
}
public void setIncludeGeneralSegments(Boolean value) {
this.includeGeneralSegments = value;
}
}
I was able to do if I remove type with actual xsd of complex object, for eg DateTimeRangeType in this case, I am using eclipse to generate jaxb classes, this is the xsd below , in earlier cases when there was no type and all elements were defined in the main parent class it was able to get the xsd format but does not seem to be working in this case where type is pointing to a complex type of xsd
<xs:element name="SegmentFilter">
<xs:annotation>
<xs:documentation>
SegmentFilter is a root-level element used
in a SegmentFilter request.
</xs:documentation>
</xs:annotation>
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="EmployeeSelector"
type="EmployeeSelectorType" minOccurs="0" />
<xs:element name="DateRange"
type="DateRangeType"/>
<xs:element name="DurationRange"
type="FromToTimeRangeType"/>
<xs:element name="SegmentCode"
type="EmptySKType"/>
<xs:element name="OverlapRange"
type="DateTimeRangeType"/>
<xs:element name="StartRange"
type="DateTimeRangeType"/>
<xs:element name="StopRange"
type="DateTimeRangeType"/>
</xs:sequence>
<xs:attribute name="IncludeDetailSegments"
default="true" type="xs:boolean"/>
<xs:attribute name="IncludeGeneralSegments"
default="true" type="xs:boolean"/>
</xs:complexType>
</xs:element>

Related

SAX parser doesn't follow references

I am trying to parse an HL7 message definition from xsd. I have my schema definition split up between two files. First file contains actual message definition and the second contains segment definitions within the message.
I am trying to tweak an example code to parse XML from here https://gist.github.com/helderdarocha/8791651. I don't understand why SAX parser doesn't follow references.
Here are two examples of my xsd definitions.
First file has the following definition
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="http://www.xsd_porcessor.org/parser"
xmlns="http://www.xsd_porcessor.org/parser"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:include schemaLocation="segments.xsd"/>
<xs:complexType name="ADT.01.MESSAGE">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH"/>
<xs:element maxOccurs="1" minOccurs="1" ref="EVN"/>
<xs:element maxOccurs="1" minOccurs="1" ref="PID"/>
<xs:element maxOccurs="1" minOccurs="1" ref="PV1"/>
<xs:element maxOccurs="1" minOccurs="1" ref="IN1"/>
<xs:element maxOccurs="1" minOccurs="1" ref="IN2"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ADT.A01" type="ADT.01.MESSAGE"/>
</xs:schema>
The second file has the following header
<?xml version="1.1" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="http://www.xsd_porcessor.org/parser"
xmlns="http://www.xsd_porcessor.org/parser"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
...and a multitude segment definitions represented as complexTypes. Bellow is example of one
<xs:complexType name="MSH.SEGMENT">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.1.FieldSeparator"/>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.2.ServiceString"/>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.3.SendingApplication"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.4.SendingFacility"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.5.ReceivingApplication"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.6.ReceivingFacility"/>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.7.DateTimeOfMessage"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.8.Security"/>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.9.MessageType"/>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.10.MessageControlID"/>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.11.ProcessingID"/>
<xs:element maxOccurs="1" minOccurs="1" ref="MSH.12.VersionID"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.13.SequenceNumber"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.14.ContinuationPointer"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.15.AcceptAcknowledgmentType"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.16.ApplicationAcknowledgmentType"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.17.CountryCode"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="MSH.18.CharacterSet"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.19.PrincipalLanguageOfMessage"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.20.AlternateCharacterSetHandlingScheme"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="MSH.21.MessageProfileIdentifier"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.22.SendingResponsibleOrganization"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.23.ReceivingResponsibleOrganization"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.24.SendingNetworkAddress"/>
<xs:element maxOccurs="1" minOccurs="0" ref="MSH.25.ReceivingNetworkAddress"/>
</xs:sequence>
</xs:complexType>
<xs:element name="MSH" type="MSH.SEGMENT"/>
Here is a tweaked parser itself
package ca.parser.xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class SAXReaderExample {
public static final String PATH = "resources";
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader reader = sp.getXMLReader();
reader.setContentHandler(new SchemaSaxHandler());
reader.parse(new InputSource(new FileInputStream(new File(PATH, "messages.xsd"))));
}
}
class SchemaSaxHandler extends DefaultHandler {
// temporary - always null when tag closes
private String currentSimpleTypeName;
private String currentSimpleTypeBaseType;
private SchemaElement currentElement;
private SchemaComplexType currentComplexType;
private List<SchemaElement> currentSequence;
// cumulative - will use the data when XML finishes
private Map<String, String> simpleTypes = new HashMap<>();
private Map<String, SchemaComplexType> complexTypes = new HashMap<>();
private SchemaElement rootElement;
#Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (qName.equals("xs:simpleType")) {
currentSimpleTypeName = atts.getValue("name");
}
if (qName.equals("xs:restriction")) {
currentSimpleTypeBaseType = atts.getValue("base");
}
if (qName.equals("xs:complexType")) {
currentComplexType = new SchemaComplexType();
currentComplexType.setName(atts.getValue("name"));
}
if (qName.equals("xs:sequence")) {
currentSequence = new ArrayList<>();
}
if (qName.equals("xs:element")) {
currentElement = new SchemaElement();
if (atts.getValue("name")==null) {
currentElement.setName(atts.getValue("ref"));
}else {
currentElement.setName(atts.getValue("name"));
}
currentElement.setType(atts.getValue("type"));
currentElement.setReference(atts.getValue("ref"));
if (currentSequence != null) {
currentSequence.add(currentElement);
} else {
rootElement = currentElement;
}
}
if (qName.equals("xs:attribute")) {
currentComplexType.addAttribute(atts.getValue("name"), atts.getValue("type"));
}
}
#Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("xs:simpleType")) {
simpleTypes.put(currentSimpleTypeName, currentSimpleTypeBaseType);
currentSimpleTypeName = null;
currentSimpleTypeBaseType = null;
}
if (qName.equals("xs:complexType")) {
complexTypes.put(currentComplexType.getName(), currentComplexType);
currentComplexType = null;
}
if (qName.equals("xs:sequence")) {
if (currentComplexType != null) {
currentComplexType.setChildren(currentSequence);
}
currentSequence = null;
}
}
#Override
public void endDocument() throws SAXException {
makeTree(rootElement);
printTree(rootElement, "");
}
public void makeTree(SchemaElement element) {
SchemaComplexType type = complexTypes.get(element.getType());
if (type != null) {
List<SchemaElement> children = type.getChildren();
element.setChildren(children);
for (SchemaElement child : children) {
makeTree(child);
}
element.setAttributes(type.getAttributes());
} else {
element.setType(simpleTypes.get(element.getType()));
}
}
private void printTree(SchemaElement element, String indent) {
System.out.println(indent + element.getName() + " : " + element.getType());
Map<String, String> attributes = element.getAttributes();
if (attributes != null) {
for (Map.Entry<String, String> entry : attributes.entrySet()) {
System.out.println(" #" + entry.getKey() + " : " + simpleTypes.get(entry.getValue()));
}
}
List<SchemaElement> children = element.getChildren();
if (children != null) {
for (SchemaElement child : children) {
printTree(child, indent + " ");
}
}
}
class SchemaElement {
private String name;
private String type;
private String reference;
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
private List<SchemaElement> children;
private Map<String, String> attributes;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<SchemaElement> getChildren() {
return children;
}
public void setChildren(List<SchemaElement> children) {
this.children = children;
}
public Map<String, String> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
}
class SchemaComplexType {
private String name;
private String reference;
private List<SchemaElement> children;
private Map<String, String> attributes = new HashMap<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<SchemaElement> getChildren() {
return children;
}
public void setChildren(List<SchemaElement> children) {
this.children = children;
}
public Map<String, String> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference=reference;
}
public void addAttribute(String name,String type) {
attributes.put(name, type);
}
}
Any ideas what is going? You help is appreciated.
Thank you.
It sounds like there are two separate concepts at work here.
If a validating SAX parser is being used to parse a piece of XML, and validate it against its schema:
<xmlRootElement
xmlns="http://www.xsd_porcessor.org/parser"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xsd_porcessor.org/parser messages.xsd">
... etc, then clearly when that schema was resolved behind the scenes, the parser would need to follow any references and imports in there.
However if the .xsd itself is the XML being parsed, then as you've already found, it's elements will be directly passed into the ContentHandler. The SchemaSaxHandler above will need to do some more work to learn each xs:element - like you're already doing for the simpleTypes and complexTypes Maps - so they can later be resolved from a ref.
If what you need is the model of the resolved elements and types in the XML schema though, it would be worth exploring that behind the scenes schema model - in an XML Parser such as Xerces. As a starting point, this is using XNI - the Xerces Native Interface:
File baseDir = new File("/myschemas");
XMLEntityResolver entityResolver = new XMLEntityResolver() {
#Override
public XMLInputSource resolveEntity(
XMLResourceIdentifier resourceIdentifier)
throws XNIException, IOException {
// E.g. resourceIdentifier.getLiteralSystemId() will be segments.xsd
String uri = new File(baseDir,
resourceIdentifier.getLiteralSystemId()).toURI()
.toString();
return new XMLInputSource(null, uri, null);
}
};
XMLSchemaLoader loader = new XMLSchemaLoader();
loader.setEntityResolver(entityResolver);
XSModel model = loader
.loadURI(new File(baseDir, "messages.xsd").toURI()
.toString());
System.out.println(model.getComponents(XSConstants.ELEMENT_DECLARATION));
This outputs such as:
{http://www.xsd_porcessor.org/parser}ADT.A01="http://www.xsd_porcessor.org/parser":ADT.A01

Weird XmlMixed XML <-> JAXB <-> JSON marshalling/unmarshalling with MOXy

I have following type, defined in schema as:
<xsd:complexType name="any_t" mixed="true">
<xsd:sequence>
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"></xsd:any>
</xsd:sequence>
</xsd:complexType>
Generated JAXB class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "any_t", propOrder = {
"content"
})
public class AnyT implements Serializable
{
private final static long serialVersionUID = 1L;
#XmlMixed
#XmlAnyElement(lax = true)
protected List<Object> content;
public List<Object> getContent() {
if (content == null) {
content = new ArrayList<Object>();
}
return this.content;
}
}
Some JAXB class that uses this type:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"addInfo"
})
#XmlRootElement(name = "FORM_OF_COOWNERS")
public class FormOfCoowners
implements Serializable
{
private final static long serialVersionUID = 1L;
#XmlElement(name = "add_info")
protected AnyT addInfo;
public AnyT getAddInfo() { return addInfo; }
public void setAddInfo(AnyT value) { this.addInfo = value; }
}
When I unmarshall this XML to JAXB object:
<FORM_OF_COOWNERS>
<add_info>
<info_analytics>
<issuer_subdiv>
<id><id>1940001</id></id>
</issuer_subdiv>
<fl_worker>Yes</fl_worker>
</info_analytics>
</add_info>
</FORM_OF_COOWNERS>
and marshall this object to JSON, I get:
{"FORM_OF_COOWNERS":{"add_info":{"info_analytics":[{"issuer_subdiv":{"id":{"id":"1940001"}},"fl_worker":"Yes"}]}}}
where does this list came from? info_analytics maps to a type without any collections/arrays.
If I get this JSON, umarshall it to JAXB object and marshall to XML, it produces:
<info_analytics fl_worker="Yes">
<issuer_subdiv>
<id id="1940001">
<id>1940001</id>
</id>
</issuer_subdiv>
<fl_worker>Yes</fl_worker>
</info_analytics>
why elements are duplicated as attributes?
UPDATE: marshalling/unmarshalling to/from json:
def unmarshallToJaxbTyped[A : ClassTag](json: String): Throwable \/ A =
\/.fromTryCatchNonFatal {
val cls = classTag[A].runtimeClass
val jc = JAXBContext.newInstance(cls)
val unmarshaller = jc.createUnmarshaller()
unmarshaller.setProperty("eclipselink.media-type", "application/json")
unmarshaller.unmarshal(new StreamSource(new StringReader(json)), cls)
.getValue.asInstanceOf[A]
}
def marshallJaxbToJson(obj: Any): Throwable \/ String =
\/.fromTryCatchNonFatal {
val jc = JAXBContext.newInstance(obj.getClass)
val marshaller = jc.createMarshaller()
marshaller.setProperty("eclipselink.media-type", "application/json")
val writer = new StringWriter()
marshaller.marshal(obj, writer)
writer.getBuffer.toString
}
xml:
protected static synchronized JAXBContext getContext() {
if (jaxbContext == null)
try {
jaxbContext = JAXBContext.newInstance("org.example.models");
} catch (JAXBException ex) {
throw new RuntimeException(ex);
}
return jaxbContext;
}
public static void saveMessage(Object msg, OutputStream os) throws JAXBException {
Marshaller marshaller = getContext().createMarshaller();
marshaller.marshal(msg, os);
}
public static Object loadMessage(InputStream is) throws JAXBException {
Unmarshaller unmarshaller = getContext().createUnmarshaller();
return unmarshaller.unmarshal(is);
}
I'm not sure how ofter such weird roundtrip: XML -> Java -> JSON -> Java -> XML is needed. I think you understand, that XML supports much more features than JSON and that's why it's not always possible to have 1 to 1 mapping.
If this is a real problem for you and not only theoretical investigations please fire a bug against MOXy: https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink

NPE when marshalling/unmarshalling JAXBElement

I have following JAXB-mapped class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "date_or_period_t", propOrder = {
"date", "beginDate", "endDate"
})
public class DateOrPeriodT
implements Serializable
{
private final static long serialVersionUID = 1L;
#XmlElementRef(name = "date", type = JAXBElement.class, required = false)
protected JAXBElement<DateOrDatetimeT> date;
#XmlElementRef(name = "begin_date", type = JAXBElement.class, required = false)
protected JAXBElement<Date> beginDate;
#XmlElementRef(name = "end_date", type = JAXBElement.class, required = false)
protected JAXBElement<Date> endDate;
public JAXBElement<DateOrDatetimeT> getDate() { return date; }
public void setDate(JAXBElement<DateOrDatetimeT> value) { this.date = value; }
public JAXBElement<Date> getBeginDate() { return beginDate; }
public void setBeginDate(JAXBElement<Date> value) { this.beginDate = value; }
public JAXBElement<Date> getEndDate() { return endDate; }
public void setEndDate(JAXBElement<Date> value) { this.endDate = value; }
}
and corresponding xsd:
<xsd:complexType name="date_or_period_t">
<xsd:choice minOccurs="0">
<xsd:element name="date" type="date_or_datetime_t" nillable="true"></xsd:element>
<xsd:sequence>
<xsd:element name="begin_date" type="xsd:date" nillable="true"></xsd:element>
<xsd:element name="end_date" type="xsd:date" nillable="true"></xsd:element>
</xsd:sequence>
</xsd:choice>
</xsd:complexType>
When one or both end_date, begin_date is set to xsi:nil="true", MOXy throws NPE:
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter.convertDataValueToObjectValue(XMLJavaTypeConverter.java:155)
at org.eclipse.persistence.oxm.mappings.converters.XMLConverterAdapter.convertDataValueToObjectValue(XMLConverterAdapter.java:28)
at org.eclipse.persistence.oxm.mappings.converters.XMLConverterAdapter.convertDataValueToObjectValue(XMLConverterAdapter.java:1)
at org.eclipse.persistence.internal.jaxb.JAXBElementConverter.convertDataValueToObjectValue(JAXBElementConverter.java:78)
at org.eclipse.persistence.internal.jaxb.JAXBElementConverter.convertDataValueToObjectValue(JAXBElementConverter.java:56)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.convertDataValueToObjectValue(XMLDirectMapping.java:526)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:296)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:1)
at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.endElement(XMLDirectMappingNodeValue.java:165)
at org.eclipse.persistence.internal.oxm.XMLChoiceObjectMappingNodeValue.endElement(XMLChoiceObjectMappingNodeValue.java:177)
at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.endElement(UnmarshalRecordImpl.java:1050)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:154)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:99)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:86)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:895)
at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:659)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:585)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:140)
When date is xsi:nil it works fine, when both begin_date and end_date is not xsi:nil it's fine.
#XmlElementDecl(namespace = "", name = "date", scope = DateOrPeriodT.class)
public JAXBElement<DateOrDatetimeT> createDateOrPeriodTDate(DateOrDatetimeT value) {
return new JAXBElement<DateOrDatetimeT>(_DateOrPeriodTDate_QNAME, DateOrDatetimeT.class, DateOrPeriodT.class, value);
}
#XmlElementDecl(namespace = "", name = "begin_date", scope = DateOrPeriodT.class)
#XmlJavaTypeAdapter(AdapterDate.class)
public JAXBElement<Date> createDateOrPeriodTBeginDate(Date value) {
return new JAXBElement<Date>(_DateOrPeriodTBeginDate_QNAME, Date.class, DateOrPeriodT.class, value);
}
#XmlElementDecl(namespace = "", name = "end_date", scope = DateOrPeriodT.class)
#XmlJavaTypeAdapter(AdapterDate.class)
public JAXBElement<Date> createDateOrPeriodTEndDate(Date value) {
return new JAXBElement<Date>(_DateOrPeriodTEndDate_QNAME, Date.class, DateOrPeriodT.class, value);
When I use the following setup, everything works fine.
class DateOrDatetimeT ->
package org.eclipse.persistence.testing.jaxb.aso.np;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "date", propOrder = {
"date"
})
public class DateOrDatetimeT {
private java.util.Date date;
public java.util.Date getDate() {
return date;
}
public void setDate(java.util.Date date) {
this.date = date;
}
#Override
public String toString() {
return "DateOrDatetimeT [date=" + date + "]";
}
}
class DateOrPeriodT ->
package org.eclipse.persistence.testing.jaxb.aso.np;
import java.io.Serializable;
import java.util.Date;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "date_or_period_t", propOrder = {
"date", "beginDate", "endDate"
})
public class DateOrPeriodT
implements Serializable {
private final static long serialVersionUID = 1L;
#XmlElementRef(name = "date", type = JAXBElement.class, required = false)
protected JAXBElement<DateOrDatetimeT> date;
#XmlElementRef(name = "begin_date", type = JAXBElement.class, required = false)
protected JAXBElement<Date> beginDate;
#XmlElementRef(name = "end_date", type = JAXBElement.class, required = false)
protected JAXBElement<Date> endDate;
public JAXBElement<DateOrDatetimeT> getDate() {
return date;
}
public void setDate(JAXBElement<DateOrDatetimeT> value) {
this.date = value;
}
public JAXBElement<Date> getBeginDate() {
return beginDate;
}
public void setBeginDate(JAXBElement<Date> value) {
this.beginDate = value;
}
public JAXBElement<Date> getEndDate() {
return endDate;
}
public void setEndDate(JAXBElement<Date> value) {
this.endDate = value;
}
#Override
public String toString() {
return "DateOrPeriodT [date=" + getJAXBElementString(date) + ", beginDate=" + getJAXBElementString(beginDate) + ", endDate=" + getJAXBElementString(endDate) + "]";
}
private String getJAXBElementString(JAXBElement<?> element) {
if (null == element) {
return null;
}
return "{" + element.getName() + ":" + element.getValue() + "}";
}
}
class ObjectFactory
package org.eclipse.persistence.testing.jaxb.aso.np;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
import java.util.Date;
#XmlRegistry
public class ObjectFactory {
#XmlElementDecl(name = "date")
JAXBElement<DateOrDatetimeT> createDate(Date date) {
DateOrDatetimeT dateOrDateTime = new DateOrDatetimeT();
dateOrDateTime.setDate(date);
return new JAXBElement<DateOrDatetimeT>(new QName("date"), DateOrDatetimeT.class, dateOrDateTime);
}
#XmlElementDecl(name = "begin_date")
JAXBElement<Date> createBeginDate(Date date) {
return new JAXBElement<Date>(new QName("begin_date"), Date.class, date);
}
#XmlElementDecl(name = "end_date")
JAXBElement<Date> createEndDate(Date date) {
return new JAXBElement<Date>(new QName("end_date"), Date.class, date);
}
}
file jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
class Test
package org.eclipse.persistence.testing.jaxb.aso.np;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
import java.util.HashMap;
public class Test {
public static void main(String[] args) throws Exception {
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/eclipse/persistence/testing/jaxb/aso/date.xml")) {
JAXBContext jc = JAXBContext.newInstance(new Class[]{DateOrPeriodT.class, DateOrDatetimeT.class, ObjectFactory.class}, new HashMap<String, Object>());
Unmarshaller marshaller = jc.createUnmarshaller();
JAXBElement el = (JAXBElement) marshaller.unmarshal(is);
DateOrPeriodT dateOrPeriodT = (DateOrPeriodT) el.getValue();
System.out.println("dateOrPeriodT=" + dateOrPeriodT);
}
}
}
different versions of date.xml (tested with EclipseLink 2.5.2)
<date_or_period_t xsi:type="date_or_period_t" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<date>2015-02-16T14:49:46.583+01:00</date>
<begin_date>2015-02-16T14:49:46.583+01:00</begin_date>
<end_date>2015-02-15T14:49:46.583+01:00</end_date>
</date_or_period_t>
<date_or_period_t xsi:type="date_or_period_t" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<date>2015-02-16T14:49:46.583+01:00</date>
<begin_date xsi:nil="true" />
<end_date>2015-02-15T14:49:46.583+01:00</end_date>
</date_or_period_t>
<date_or_period_t xsi:type="date_or_period_t" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<date>2015-02-16T14:49:46.583+01:00</date>
<begin_date xsi:nil="true" />
<end_date xsi:nil="true" />
</date_or_period_t>
<date_or_period_t xsi:type="date_or_period_t" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<begin_date xsi:nil="true" />
<end_date>2015-02-15T14:49:46.583+01:00</end_date>
</date_or_period_t>
<date_or_period_t xsi:type="date_or_period_t" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<begin_date xsi:nil="true" />
<end_date xsi:nil="true" />
</date_or_period_t>
<date_or_period_t xsi:type="date_or_period_t" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<begin_date>2015-02-15T14:49:46.583+01:00</begin_date>
<end_date>2015-02-15T14:49:46.583+01:00</end_date>
</date_or_period_t>
If you use EclipseLink 2.6.0, xml needs nested date element:
<date_or_period_t xsi:type="date_or_period_t" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<date><date>2015-02-16T14:49:46.583+01:00</date></date>
<begin_date>2015-02-16T14:49:46.583+01:00</begin_date>
<end_date>2015-02-15T14:49:46.583+01:00</end_date>
</date_or_period_t>

Trouble deserializing a document containing xsi:type attributes

When parsing XML file I have error in serializer.Deserialize(reader) :
Namespace prefix \"java\" not defined
my XML http://www.virtualdtm.ru/files/serverReplays/rFactorHotlapsData.xml
my code
var serializer = new XmlSerializer(typeof(rootDB));
var reader = new XmlTextReader(Server.MapPath(Url.Content("~/Content/xml/rFactorHotlapsData.xml")));
var rFactorHotLapsData = (rootDB)serializer.Deserialize(reader);
my class
//------------------------------------------------------------------------------
// <auto-generated>
// Этот код создан программой.
// Исполняемая версия:4.0.30319.17929
//
// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае
// повторной генерации кода.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
namespace net.vmso.cpanel.Models.rFactorHotLaps
{
}
//
// Этот исходный код был создан с помощью xsd, версия=4.0.30319.17929.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class rootDB {
private rootDBListOfCategory[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("listOfCategory", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public rootDBListOfCategory[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDBListOfCategory {
private string categoryNameField;
private rootDBListOfCategoryListOfTrack[] listOfTrackField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string categoryName {
get {
return this.categoryNameField;
}
set {
this.categoryNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("listOfTrack", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootDBListOfCategoryListOfTrack[] listOfTrack {
get {
return this.listOfTrackField;
}
set {
this.listOfTrackField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDBListOfCategoryListOfTrack {
private string trackNameField;
private rootDBListOfCategoryListOfTrackSlowestLapRecord[] slowestLapRecordField;
private rootDBListOfCategoryListOfTrackListOfLapRecord[] listOfLapRecordField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string trackName {
get {
return this.trackNameField;
}
set {
this.trackNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("slowestLapRecord", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootDBListOfCategoryListOfTrackSlowestLapRecord[] slowestLapRecord {
get {
return this.slowestLapRecordField;
}
set {
this.slowestLapRecordField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("listOfLapRecord", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootDBListOfCategoryListOfTrackListOfLapRecord[] listOfLapRecord {
get {
return this.listOfLapRecordField;
}
set {
this.listOfLapRecordField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDBListOfCategoryListOfTrackSlowestLapRecord {
private string driverNameField;
private string sessionNameField;
private string dateField;
private string vehicleModNameField;
private string vehicleNameField;
private string vehicleCategoryField;
private string lapTimeField;
private string lastLapTimeField;
private string rankField;
private string sessionIdField;
private string lapCountField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string driverName {
get {
return this.driverNameField;
}
set {
this.driverNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string sessionName {
get {
return this.sessionNameField;
}
set {
this.sessionNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string date {
get {
return this.dateField;
}
set {
this.dateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string vehicleModName {
get {
return this.vehicleModNameField;
}
set {
this.vehicleModNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string vehicleName {
get {
return this.vehicleNameField;
}
set {
this.vehicleNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string vehicleCategory {
get {
return this.vehicleCategoryField;
}
set {
this.vehicleCategoryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lapTime {
get {
return this.lapTimeField;
}
set {
this.lapTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lastLapTime {
get {
return this.lastLapTimeField;
}
set {
this.lastLapTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string rank {
get {
return this.rankField;
}
set {
this.rankField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string sessionId {
get {
return this.sessionIdField;
}
set {
this.sessionIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lapCount {
get {
return this.lapCountField;
}
set {
this.lapCountField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDBListOfCategoryListOfTrackListOfLapRecord {
private string driverNameField;
private string sessionNameField;
private string dateField;
private string vehicleModNameField;
private string vehicleNameField;
private string vehicleCategoryField;
private string lapTimeField;
private string lastLapTimeField;
private string rankField;
private string sessionIdField;
private string lapCountField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string driverName {
get {
return this.driverNameField;
}
set {
this.driverNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string sessionName {
get {
return this.sessionNameField;
}
set {
this.sessionNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string date {
get {
return this.dateField;
}
set {
this.dateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string vehicleModName {
get {
return this.vehicleModNameField;
}
set {
this.vehicleModNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string vehicleName {
get {
return this.vehicleNameField;
}
set {
this.vehicleNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string vehicleCategory {
get {
return this.vehicleCategoryField;
}
set {
this.vehicleCategoryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lapTime {
get {
return this.lapTimeField;
}
set {
this.lapTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lastLapTime {
get {
return this.lastLapTimeField;
}
set {
this.lastLapTimeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string rank {
get {
return this.rankField;
}
set {
this.rankField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string sessionId {
get {
return this.sessionIdField;
}
set {
this.sessionIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lapCount {
get {
return this.lapCountField;
}
set {
this.lapCountField = value;
}
}
}
[1]:
The java namespace prefix is not defined, but it is used in the xsi:type attributes of your .xml document. If you add an attribute that defines it it to the root of your document, the error will go away.
<rootDB xmlns:java="http://tempuri3.org">
As we later found out, on top of this the xsi:type is messing up the generation of the classes. Badly.
Using XmlSerializer
<listOfTrack xsi:type="java:com.prorfactor.top100.database.Track">
The name of the element is listOfTrack, in the default namespace. Its type however is com.prorfactor.top100.database.Track in the namespace the java prefix is referring to. This happens because the xsi:type attribute changes it. xsd.exe can deal with this, but it needs a little help.
The way I got it to work is to create two .xsds. One for the java namespace and the other for the namespace of the document. They import each other.
As an example I'll show the bit that describes the start of the document where the names and types hop around a lot.
<rootDB xmlns="http://tempuri2.org"
xmlns:java="http://tempuri3.org">
<listOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:com.prorfactor.top100.database.Category">
<listOfTrack xsi:type="java:com.prorfactor.top100.database.Track">
For the default namespace, which I called http://tempuri2.org, the schema has to list the names of the elements, because they are in the default namespace:
<xs:element name="rootDB" >
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="listOfCategory" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="listOfCategory"
type="java:com.prorfactor.top100.database.Category">
</xs:element>
<xs:element name="listOfTrack"
type="java:com.prorfactor.top100.database.Track">
</xs:element>
The schema for the java namespace, which I called http://tempuri3.org lists the complextypes that are assigned to the elements using xsi:type:
<xs:complexType name="com.prorfactor.top100.database.Category"
xmlns:orig="http://tempuri2.org">
<xs:sequence>
<xs:element ref="orig:listOfTrack" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="orig:categoryName"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="com.prorfactor.top100.database.Track"
xmlns:orig="http://tempuri2.org">
<xs:sequence>
<xs:element ref="orig:slowestLapRecord" />
<xs:element ref="orig:listOfLapRecord" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="orig:trackName"/>
</xs:sequence>
</xs:complexType>
These two .xsds I compiled and the generated classes manage to deserialize the .xml file.
IF I manually add the java namespace, plus specify the default namespace to the .xml file
So, to be honest, I think you're better off using LINQ to XML:
Using LINQ to XML
XDocument xElement = XDocument.Load(Server.MapPath(Url.Content("~/Content/xml/rFactorHotlapsData.xml")));
// just some selects to show how you select them
var cats = from cat in xElement.Descendants("listOfCategory")
select new
{
Name = cat.Descendants("categoryName").FirstOrDefault().Value,
Track = from track in cat.Descendants("listOfTrack")
select new
{
Name = track.Descendants("trackName").FirstOrDefault().Value,
LapRecord = from record in cat.Descendants("listOfLapRecord")
select new
{
driverName = record.Descendants("driverName").FirstOrDefault().Value,
sessionName = record.Descendants("sessionName").FirstOrDefault().Value,
date = record.Descendants("date").FirstOrDefault().Value
}
}
};
// And a demo of how to iterate over the result of the select
foreach (var category in cats)
{
Console.WriteLine(category.Name);
foreach (var track in category.Track)
{
Console.WriteLine(" " + track.Name);
foreach (var lapRecord in track.LapRecord)
{
Console.WriteLine(" " + lapRecord.driverName);
Console.WriteLine(" " + lapRecord.sessionName);
Console.WriteLine(" " + lapRecord.date);
}
}
}

JAXB difference between #XmlSchemaType(name="normalizedString") and #XmlJavaTypeAdapter(NormalizedStringAdapter.class)

I'm writing a JAXB class without XSD-compilation.
Can anybody please tell me the difference between
#XmlSchemaType(name = "normalizedString")
private String normalized;
and
#XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
?
#XmlSchemaType(name = "normalizedString")
private String normalized;
When the above annotation is used, the xsd:normalizedString type will be specified for the attribute/element corresponding to this property when an XML schema is generated.
#XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;
The NormalizedStringAdapter does the work during the unmarshal operation: line feeds, carriage returns, and tab characters are removed.
Example
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<adapter> A B
C </adapter>
<schemaType> A B
C </schemaType>
<control> A B
C </control>
</root>
Root
package forum383861;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
#XmlRootElement
#XmlType(propOrder={"adapter", "schemaType", "control"})
public class Root {
private String adapter;
private String schemaType;
private String control;
#XmlJavaTypeAdapter(NormalizedStringAdapter.class)
public String getAdapter() {
return adapter;
}
public void setAdapter(String adpater) {
this.adapter = adpater;
}
#XmlSchemaType(name="normalizedString")
public String getSchemaType() {
return schemaType;
}
public void setSchemaType(String schemaType) {
this.schemaType = schemaType;
}
public String getControl() {
return control;
}
public void setControl(String control) {
this.control = control;
}
}
Demo
package forum383861;
import java.io.*;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum383861/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
System.out.println();
jc.generateSchema(new SchemaOutputResolver() {
#Override
public Result createOutput(String arg0, String arg1) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(arg1);
return result;
}
});
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<adapter> A B C </adapter>
<schemaType> A B
C </schemaType>
<control> A B
C </control>
</root>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:element name="adapter" type="xs:string" minOccurs="0"/>
<xs:element name="schemaType" type="xs:normalizedString" minOccurs="0"/>
<xs:element name="control" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

Resources