how to detect unexpected element in jaxb - java

I use JAXB 2.2.7 to get data from my XML files, the problem is I need to check if there are some unexpected element in my XML files but JAXB don't care and work perfectly without any error.
Example :
<classe>
<detail>
<knowElementFromXSD>value</knowElementFromXSD>
<unknowElementFromXSD>OtherValue</unknowElementFromXSD>
</detail>
</classe>
For example I need JAXB to throw an error for the in this XML file because isn't define in my XSD.
If someone have an idea, I can't find any solution on the internet.

There are a couple of different ways to accomplish your use case:
Option #1 - ValidationEventHandler
You can set a ValidationEventHandler on your Unmarshaller to be notified of things like unexpected elements. By default a JAXB (JSR-222) implementation will just ignore unmapped elemeents.
Option #2 - Schema Validation
If you set an instance of javax.xml.validation.Schema on the Unmarshaller then the XML input will be validated as it is unmarshalled.
http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html

Related

xstream is not parsing root element

I am using 'com.thoughtworks.xstream:xstream:1.4.10' library and trying to parse xml files.
Broker is the root element and there are other tags inside
<broker>
<othertags/>
</broker>
The problem is when I am generating an xml file it is generating properly but it is not able read a file.
#XStreamAlias("broker")
public static class Broker {
While file generation it is able to convert Broker class to but not the other way around. All other classes and list are getting correctly mapped but the root #XStreamAlias is not working while reading.
Any pointers as to why will be very helpful.
The exception I am getting:
com.thoughtworks.xstream.mapper.CannotResolveClassException: broker
One more question: while calling xStream.fromXML(responseString) how does xStream know which class to use? Say I have two classes with same alias
XStream does not process annotations by default.
Add the following before the deserialization of your xml content.
XStream xstream = new XStream();
xstream.processAnnotations(Broker.class);

Apache CXF parse WSDL Schema xsd:any type

I am trying to find good and most generic solution for soap web services' problem. The problem I need to solve is to replace proxies which Apache Axis generate using given WSDL file with the proxies that Apache CXF genereta using wsdl2java command.
However, when I generate proxies with CXF and opened the classes in IDE I realised that I have:
#XmlAnyElement(lax = true)
protected List<Object> any;
element as a Class field. This was strange. I looked at the pure wsdl and in one of the complextype properties there is xsd:any element. I have been searching for a long time what is the problem and for the best solution to solve it. I found this . It seems that apache CXF runtime cannot determine the actual data type of the element. So I need to parse it manually with DOM parser.
Apache Axis generate the following:
private org.apache.axis.message.MessageElement[] _any;
The tag:
<any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
My question is: What is the best way to solve this problem? Do I really need DOM parser to ? Is there something that i miss ?
Thank you.
<xsd:any> is used to allow in the schema any element. See this link
The element enables us to extend the XML document with elements not specified by the schema.
So mapping of CXF is correct. The JAXB annotation #XmlAnyElement(lax = true) means that for that field if contains an element annotated with #XmlRootElement or #XmlElementDecl then an instance of the corresponding class will be used to populate the field if not the element will be set as an instance of org.w3c.dom.Element.
See an example here

How to Creating Nested xml in Jaxb. I need to create xml as below

I need to create xml as below using jaxb. I know how to create simle xml.But I have no idea regarding this nested xml.Please help me out with this. Thank you all
<whens>
<when>
<whenEntity1_Rule1>
<whenAttribute1_Rule1>
<whenCondition1_Rule1/>
<whenValue1_Rule1>
<literalvalue1_Rule1/>
<whenEntity1_expression1_Rule1/>
<whenAttribute1_expression1_Rule1/>
</whenValue1_Rule1>
</whenAttribute1_Rule1>
<whenAttribute2_Rule1>
<whenCondition2_Rule1/>
<whenValue2_Rule1>
<literalvalue2_Rule1/>
<whenEntity2_expression1_Rule1/>
<whenAttribute2_expression1_Rule1/>
</whenValue2_Rule1>
</whenAttribute2_Rule1>
<whenAttribute3_Rule1>
<whenCondition3_Rule1/>
<whenValue3_Rule1>
<literalvalue3_Rule1/>
<whenEntity3_expression1_Rule1/>
<whenAttribute3_expression1_Rule1/>
</whenValue3_Rule1>
</whenAttribute3_Rule1>
</whenEntity1_Rule1>
</when>
<whens>
You can use an XSD generator to extract the schema that describes the structure you've mentioned above.
You will then be add to add this schema to a JAXB marshaller or unmarshaller to generate JAXB objects and XML that is valid against the above structure. You can see how to do this in the answer to this question.

JAXP: How to dynamically resolve a Schema during XML parsing?

Imagine a xml file which refers to a schema using noNamespaceSchemaLocation.
I'd like to resolve the value defined in noNamespaceSchemaLocation dynamically using some kind of resolving technique (like ResourceResolver for resolving schema includes) during parsing (default values in the schema have to be considered)
Is there a way to do this?
I would guess that when you call Validator.setResourceResolver(), the LSResourceResolver you supply is used for this purpose (it is in the Saxon implementation of the JAXP interface, but you would need to run a test to check that it's also true of the Xerces implementation).

How should I use Stax2 Validation API against a W3 Schema

I am using com.ctc.wstx.stax.WstxOutputFactory to generate XML.
I am running wstx-asl-3.2.4
I need to start validating the generated XML against a W3 Schema.
When I create an instance of org.codehaus.stax2.validation.XMLValidationSchemaFactory like this
private final static XMLValidationSchemaFactory xsdFact=
XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
I get the error
javax.xml.stream.FactoryConfigurationError: No XMLValidationSchemaFactory implementation class specified or accessible (via system property 'org.codehaus.stax2.validation.XMLValidationSchemaFactory.w3c', or service definition under 'META-INF/services/org.codehaus.stax2.validation.XMLValidationSchemaFactory.w3c')
at org.codehaus.stax2.validation.XMLValidationSchemaFactory.newInstance(XMLValidationSchemaFactory.java:208)
at org.codehaus.stax2.validation.XMLValidationSchemaFactory.newInstance(XMLValidationSchemaFactory.java:98)
I can see that woodstox is bundled with a DTD parser only.
I found this article
which contains the unhelpful instruction
Get an instance of XMLValidationSchemaFactory that knows how to parse schemas of the type you need (RelaxNG == rng for this example).
I have been looking at the Sun Multi-Schema XML Validator which is supposed to contain the bits necessary to bolt on to the XMLSchemaValidation factory.
It looks like I might be able to use com.sun.msv.reader.xmlschema.XMLSchemaReader
to write my own instance of XMLValidationSchemaFactory and get it to work this way.
My question is; do I really have to do this, or is there a pre-existing w3c schema factory that I have failed to find?
Perhaps it would be simpler just to validate the XML after I have generated it.
What are the views on this ?
I've upgraded to Woodstox 4.0.8, W3CSchemaFactory comes bundled and its all good.

Categories