I have an xml:
<package>
<metadata>
<title>Title</title>
<language>en</language>
<meta name="cover" />
</metadata>
</package>
Another examples of correct xml's:
<package>
<metadata>
<language>en</language>
<title>Title</title>
</metadata>
</package>
<package>
<metadata>
<meta name="content" />
<language>en</language>
<meta name="cover" />
<title>Title</title>
</metadata>
</package>
And I want to unmarshall it to the java class using JAXB library.
The difficulty is that amount of "meta" element can be from 0 to multiple and order of elements "title" "language" and "meta"s can also be random.
My class looks like:
#XmlRootElement(name = "package")
#XmlAccessorType(XmlAccessType.FIELD)
public class Test {
#XmlElement(name = "metadata", required = true)
public Metadata metadata;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(propOrder={})
public static class Metadata {
#XmlElement(name = "title", required = true)
public String title;
#XmlElement(name = "language", required = true)
public String language;
#XmlElement(name = "meta", required = false)
public List<Meta> metas;
}
#XmlAccessorType(XmlAccessType.FIELD)
public static class Meta {
#XmlAttribute(name = "name", required = true)
public String name;
}
}
It matches the schema:
<?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="package" type="test"/>
<xs:complexType name="test">
<xs:sequence>
<xs:element name="metadata" type="metadata"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="metadata">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="meta" type="meta" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="meta">
<xs:sequence/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
When I unmarshall the document I get an error:
org.xml.sax.SAXParseException: cos-all-limited.2: The {max occurs} of an element in an 'all' model group must be 0 or 1. The value 'unbounded' for element 'meta' is invalid.
If I remove the attribute #XmlType(propOrder={}) it matches the schema:
<?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="package" type="test"/>
<xs:complexType name="test">
<xs:sequence>
<xs:element name="metadata" type="metadata"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="metadata">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="language" type="xs:string"/>
<xs:element name="meta" type="meta" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="meta">
<xs:sequence/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
And I get an error if the order of the properties is different:
Invalid content was found starting with element 'language'. One of '{title}' is expected.
I can not change the xml files, but I can modify the classes and/or annotations. I generate schema by this classes dynamically, so the schema is also can be adjust.
Any ideas how to achieve a desirable behaviour?
Related
I used Jaxb to generate a Jar file from xsd:
CustomObject inputRequest = xmlMapper.readValue(input, CustomObject.class);
The error message I am getting is:
com.fasterxml.jackson.databind.JsonMappingException: Multiple fields
representing property "PS": OrderLineType#pS vs OrderLineType#pSV5 at
[Source: (StringReader); line: 1, column: 1]
And my class is:
#XmlElement(name = "PS", namespace = "financetypes:defn:v4")
protected financetypes.v4.PSType pS;
#XmlElement(name = "PS", namespace = "financetypes:defn:v5")
protected financetypes.v5.PSType pSV5;
They have the same name, but different namespace.
Is xmlmapper able to handle XmlElement that has the same name and different namespace?
If yes, how do I fix this? If no, what should I do?
I am building jar file from jenkin. Is there a plugin that I can use to fix this issue?
Thanks!
here is my xsd
v4.xsd
<!--Begin Definition: Complex Type: PriceStructureType-->
<xs:complexType name="PriceStructureType">
<xs:sequence>
<xs:element name="PriceModel" type="xs:string" minOccurs="0">
<xs:annotation>
<xs:documentation>
Will be required in future...
TODO: Add valid items...
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="finance:SourceSystemPrice" minOccurs="0">
<xs:annotation>
<xs:documentation>
Will be required in future...
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="ComponentPrice" type="finance:ComponentListType" minOccurs="0">
<xs:annotation>
<xs:documentation>
Will be required in future...
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="PriceStructure" type="finance:PriceStructureType"/>
<!--End Definition: Complex Type: PriceStructureType-->
v5.xsd
<!--Begin Definition: Complex Type: PriceStructureType-->
<xs:complexType name="PriceStructureType">
<xs:sequence>
<xs:element name="PriceModel" type="xs:string" minOccurs="0">
<xs:annotation>
<xs:documentation>
Will be required in future...
TODO: Add valid items...
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="finance:SourceSystemPrice" minOccurs="0">
<xs:annotation>
<xs:documentation>
Will be required in future...
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="ComponentPrice" type="finance:ComponentListType" minOccurs="0">
<xs:annotation>
<xs:documentation>
Will be required in future...
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="PriceStructure" type="finance:PriceStructureType"/>
<!--End Definition: Complex Type: PriceStructureType-->
common.xsd
xmlns:finance="urn:financetypes:defn:v4"
xmlns:finance_v5="urn:financetypes:defn:v5"
<xs:import namespace="urn:financetypes:defn:v4" schemaLocation="financetypes.v4.xsd"/>
<xs:import namespace="urn:financetypes:defn:v5" schemaLocation="financetypes.v5.xsd"/>
<xs:choice>
<xs:element ref="finance:PriceStructure" minOccurs="0">
<xs:annotation>
<xs:documentation xml:lang="en">
Price Structure itemizes the price components of this OrderLine. Pricing
is a integral part of the Sales Order record.
This should be a required element for a sales order, but it is marked
as optional for backward compatibility.
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="finance_v5:PriceStructure" minOccurs="0">
<xs:annotation>
<xs:documentation xml:lang="en">
Price Structure itemizes the price components of this OrderLine. Pricing
is a integral part of the Sales Order record.
This should be a required element for a sales order, but it is marked
as optional for backward compatibility.
</xs:documentation>
<xs:appinfo>
<jaxb:property name="PriceStructureV5"/>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:choice>
You could do this. For a root class containing these 2 elements:
#XmlRootElement(name = "root")
#XmlAccessorType(XmlAccessType.FIELD)
public class Root {
#XmlElement(name = "PS", namespace = "financetypes:defn:v4")
protected financetypes.v4.PSType pS;
#XmlElement(name = "PS", namespace = "financetypes:defn:v5")
protected financetypes.v5.PSType pSV5;
}
You can create you classes with the different versions like this:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "v4PS", namespace = "financetypes:defn:v4")
public class PSType {
#XmlValue
private String value;
}
and:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "v5PS", namespace = "financetypes:defn:v5")
public class PSType {
#XmlValue
private String value;
}
For a sample xml as below:
<root xmlns:v4="financetypes:defn:v4" xmlns:v5="financetypes:defn:v5">
<v4:PS>version 4</v4:PS>
<v5:PS>version 5</v5:PS>
</root>
You will be able to unmarshall properly.
Update to respond to the comment:
You use xsd to generate the classes. You did not provide the xsd so I will assume you are not allowed to. I created an xsd to generate the classes you show in your question. The namespace.xsd looks like this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:v4="financetypes:defn:v4" xmlns:v5="financetypes:defn:v5">
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element name="v4PSType" type="PSTypev4" />
<xs:element name="v5PSType" type="PSTypev5" />
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name="PSTypev4">
<xs:simpleContent>
<xs:extension base="xs:string" />
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="PSTypev5">
<xs:simpleContent>
<xs:extension base="xs:string" />
</xs:simpleContent>
</xs:complexType>
</xs:schema>
Then your bindings would be:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:bindings schemaLocation="../xsd/namespaces.xsd">
<jaxb:bindings node="//xs:complexType[#name='PSTypev4']">
<annox:annotate target = "class">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlType" name="v4PSType" namespace="financetypes:defn:v4" />
</annox:annotate>
</jaxb:bindings>
<jaxb:bindings node="//xs:complexType[#name='PSTypev5']">
<annox:annotate target = "class">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlType" name="v5PSType" namespace="financetypes:defn:v5" />
</annox:annotate>
</jaxb:bindings>
<jaxb:bindings node="//xs:element[#name='root']//xs:complexType//xs:all//xs:element[#name='v5PSType']">
<annox:annotate target = "field">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlElement" name="PS" namespace="financetypes:defn:v5" />
</annox:annotate>
</jaxb:bindings>
<jaxb:bindings node="//xs:element[#name='root']//xs:complexType//xs:all//xs:element[#name='v4PSType']">
<annox:annotate target = "field">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlElement" name="PS" namespace="financetypes:defn:v4" />
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
And then you would end up with classes as those in my answer.
I've made a mistake but I can't see what it is...
I'm trying to validate a piece of XML (a SOAP message) using Java, against an XSD. It's failing on what I think should be a valid piece of XML/message with the Exception:
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'tns:Envelope'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203).
Note 1: I've debugged xmlSource and xsdSource and they contain the expected Readers with the expected Strings.
Note 2: The content of the XML cannot be changed. I am a consumer only.
Note 3: As the XML is a SOAP message I grabbed the XSD from the Web. This therefore could be incorrect.
Note 4: I've simplified the incoming message for brevity but get the same error with the "actual" message.
Java
#Override
public boolean isMessageValid(final String xml) {
try {
final Source xmlSource = getStreamSource(xml);
final Source xsdSource = getStreamSource(fileService.getResourceFileAsString("xsd/soap-envelope.xsd"));
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(xsdSource);
final Validator validator = schema.newValidator();
validator.validate(xmlSource);
return true;
} catch (final Exception ex) {
LOG.error("Exception whilst validating message", ex);
return false;
}
}
private StreamSource getStreamSource(final String xml) {
return new StreamSource(new StringReader(xml));
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<tns:Envelope xsi:schemaLocation="http://www.w3.org/2001/12/soap-envelope soap-envelope.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://www.w3.org/2001/12/soap-envelope">
<tns:Body>
<!-- snipped -->
<someThing>else</someThing>
</tns:Body>
</tns:Envelope>
XSD
<?xml version='1.0' encoding='UTF-8' ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/"
targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" >
<!-- Envelope, header and body -->
<xs:element name="Envelope" type="tns:Envelope" />
<xs:complexType name="Envelope" >
<xs:sequence>
<xs:element ref="tns:Header" minOccurs="0" />
<xs:element ref="tns:Body" minOccurs="1" />
<xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##other" processContents="lax" />
</xs:complexType>
<xs:element name="Header" type="tns:Header" />
<xs:complexType name="Header" >
<xs:sequence>
<xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##other" processContents="lax" />
</xs:complexType>
<xs:element name="Body" type="tns:Body" />
<xs:complexType name="Body" >
<xs:sequence>
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##any" processContents="lax" >
<xs:annotation>
<xs:documentation>
Prose in the spec does not specify that attributes are allowed on the Body element
</xs:documentation>
</xs:annotation>
</xs:anyAttribute>
</xs:complexType>
<!-- Global Attributes. The following attributes are intended to be usable via qualified attribute names on any complex type referencing them. -->
<xs:attribute name="mustUnderstand" >
<xs:simpleType>
<xs:restriction base='xs:boolean'>
<xs:pattern value='0|1' />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="actor" type="xs:anyURI" />
<xs:simpleType name="encodingStyle" >
<xs:annotation>
<xs:documentation>
'encodingStyle' indicates any canonicalization conventions followed in the contents of the containing element. For example, the value 'http://schemas.xmlsoap.org/soap/encoding/' indicates the pattern described in SOAP specification
</xs:documentation>
</xs:annotation>
<xs:list itemType="xs:anyURI" />
</xs:simpleType>
<xs:attribute name="encodingStyle" type="tns:encodingStyle" />
<xs:attributeGroup name="encodingStyle" >
<xs:attribute ref="tns:encodingStyle" />
</xs:attributeGroup>
<xs:element name="Fault" type="tns:Fault" />
<xs:complexType name="Fault" final="extension" >
<xs:annotation>
<xs:documentation>
Fault reporting structure
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="faultcode" type="xs:QName" />
<xs:element name="faultstring" type="xs:string" />
<xs:element name="faultactor" type="xs:anyURI" minOccurs="0" />
<xs:element name="detail" type="tns:detail" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="detail">
<xs:sequence>
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
</xs:sequence>
<xs:anyAttribute namespace="##any" processContents="lax" />
</xs:complexType>
</xs:schema>
I reckon I've done something noddy.
Thanks in advance
The prefix "tns" is bound to different namespaces in the source and in the schema. So this is not a schema for the vocabulary of your source document.
I have a JAXB class, which has an element coming from another namespace. I would like to have an ANY element as well, which collects all the elements not described in the JAXB class. So I added a List<Object> annotated with the #XmlAnyElement.
I have generated an XSD from the JAXB class, but when I am validating I am getting an error: "cos-nonambig: "nb":b and WC[##other:""] (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles."
The JAXB class
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType
public class Foo {
#XmlElement
String a;
#XmlElement(namespace="nb")
Bar b;
#XmlAnyElement
List<Object> other;
}
The XSD generated by JAXB
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:ns1="nb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="nb" schemaLocation="schema1.xsd"/>
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="0"/>
<xs:element ref="ns1:b" minOccurs="0"/>
<xs:any processContents="skip" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="bar">
<xs:sequence>
<xs:element name="bid" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="nb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="schema2.xsd"/>
<xs:element name="b" type="bar"/>
</xs:schema>
How should I add the XmlAnyElement to pass the validation?
i have simple xsd file:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and some code to parse it
String aFile = "C:\\1.xsd";
XSDParser parser=null;
XSDSchema schema;
URI schemaUri = new File(aFile).toURI();
String uri;
try {
uri = schemaUri.toURL().toString();
parser=XSDParser.class.newInstance();
parser.parse(uri);
schema = parser.getSchema();
for (XSDElementDeclaration element : schema.getElementDeclarations()) {
System.out.println(element.getName());
}
Program prints "note". I debuged it, and can not find element to,from,heading and body. what do I change to receive output:
note
to
from
heading
body
org.eclipse.xsd.util.XSDPrototypicalSchema
Builds instances is likely to help you understand how to traverse them.
I have the following XML file:
<?xml version="1.0"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library
xmlns="http://example.com/a"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com library.xsd"
name=".NET Developer's Library">
<book>
<category>computerss</category>
<title>Programming Microsoft .NET</title>
<author>Jeff Prosise</author>
<isbn>0-7356-1376-1</isbn>
</book>
<book>
<category>computer</category>
<title>Microsoft .NET for Programmers</title>
<author>Fergal Grimes</author>
<isbn>1-930110-19-7</isbn>
</book>
</library>
And the following Java code:
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
docBuilderFactory.setSchema(sf.newSchema(new File("library.xsd")));
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.parse(new FileInputStream("data.xml"));
It produces the following error:
[Error] :7:33: cvc-elt.1: Cannot find the declaration of element 'library'.
If I remove the XSD declaration in the XML file everything works fine...
Any inside highly appreciated. Thanks.
And here is the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="category"/>
<xs:element ref="title"/>
<xs:element ref="author"/>
<xs:element ref="isbn"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="computer" />
<xs:enumeration value="poetry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:schema>
Your XML has a reference to namespace (xmlns="http://example.com/a") which is not the same as in your schema. Have you tried to validate your XML against schema in any XML editor (e.g. Altova or Eclipse etc).
So far it looks like your parsing error is legit, XML is not valid according to the schema.
Your schema definition is incorrect. For a start, as maximdim says, your schema has no targetNamespace="http://mysite.com/a" attribute in the schema tag.
Secondly your schema looks as if it should only have a single root element, yours has 6.
A correct schema for your XML instance would be:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://mysite.com/a" targetNamespace="http://mysite.com/a">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="book" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="book">
<xs:sequence>
<xs:element name="category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="computer" />
<xs:enumeration value="poetry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="isbn" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>