JAXB 2.0 Validation Question - java

While working with JAXB 2.0 i came across a query which i am unable to solve so far,while doing the validation i have 2 options
1) Either as soon as i found the error throw the exception as i am done.
2) Move ahead if there is any error or validation and i assume this is the best way since it will help one to show all errors or warings with respect to the whole XML.
but since this process also invlolves unmarshalling means it will unmarshall my provided XML is to respected Object even if there is any Error or warning.so all means extra work..
My question is is these a way so that i can do whole validation and if it is successfull only than should the corresponding XML be bind to respected POJO classes
thanks in advance

You can use the javax.xml.validation APIs to validate an XML document against an XML schema. The you can choose to unmarshal this object again using JAXB.
Below is an example of using these APIs. In this example the input is actually an object model, but you can adapt it to work with any XML input.
http://bdoughan.blogspot.com/2010/11/validate-jaxb-object-model-with-xml.html

Related

JAXB issue with missing namespace definition

So I searched around quite a bit for a solution to this particular issue and I am hoping someone can point me in a good direction.
We are receiving data as XML, and we only have XSD to validate the data. So I used JAXB to generate the Java classes. When I went to unmarshal a sample XML, I found that some attribute values are missing. It turns out that the schema expects those attributes to be QName, but the data provider didn't define the prefix in the XML.
For instance, one XML attribute value is "repository:<uuid>", but the namespace prefix "repository" is never defined in the dataset. (Never mind the provider's best practices suggest defining it!)
So when I went to unmarshal a sample set, the QName attributes with the specified prefix ("repository" in my sample above) are NULL! So it looks like JAXB is "throwing out" those attribute QName values which have undefined namespace prefix. I am surprised that it doesn't preserve even the local name.
Ideally, I would like to maintain the value as is, but it looks like I can't map the QName to a String at binding time (Schema to Java).
I tried "manually" inserting a namespace definition to the XML and it works like a charm. What would be the least complicated method to do this?
Is there a way to "insert" namespace mapping/definition at runtime? Or define it "globally" at binding time?
The simplest would be to use strings instead of QName. You can use the javaType customization to achieve this.
If you want to add prefix/namespace mappings in the runtime, there are quite a few ways to do it:
Similar to above, you could provide your own QName converter which would consider your prefixes.
You can put a SAX or StAX filter in between and declare additional prefixes in the startDocument.
What you actually need is to add your prefix mappings into the UnmarshallingContext.environmentNamespaceContext. I've checked the source code but could not find a direct and easy way to do it.
I personally would implement a SAX/StAX filter to "preprocess" your XML on the event level.

Is there a way of handling multiple xsd versions with xmlBeans?

I know that I can compile multiple xsd files in a single jar. I've tried using different namespaces which only takes me half way through my goal. This way I can parse the correct schema but I want this to be transparent to my users which will receive the xmlBeans object that I've parsed.
They don't have to know which version of xml file is currently present on the system. I would need a super class for every xsd version to achieve this.
Could this be done with xmlBeans?
My understanding is, if you have a com namespace and a com.v1 and com.v2 namespace and you have an xsd element called EmployeeV1 in com.v1 and EmployeeV2 in com.v2.
You want to a super class called Employee in the com namespace which you want to return to your caller?
Do you think EmployeeV1 and EmployeeV2 could extend from Employee in your xsd? Then maybe when you generate you will get the class hierarchy that represents your xsd.
If that doesn't work, (i haven't used xmlbeans in years now), you might have to create your own domain object and make your callers consume that. That might be worth the effort, since to me it looks like you handle the parsing of an XML that other people rely on, you could abstract all other users from the structure of the XML (which is in flux) by having an intermediary domain object.

Decoupling XML parser from classes

I'm writing an Android application and I want to provide the ability for the end user to create collections of objects from a class hierarchy with XML. For example: I'd like the user to be able to provide something like this arbitrary example:
<animals>
<dogs>
<bulldog name="Frank" />
<mastif name="George" />
</dogs>
<cats>
<mainecoon name="Alex" age="3" />
</cats>
</animals>
I'd like to validate this user-provided XML against a schema I create, and then create instances of these classes based on the XML. The question I have is that if I add a subclass.. say reptile, I have to change the implementation for the XML parser as well as the schema to validate the XML. This seems like bad practice because a change in the class hierarchy mandates a change in other places. Is there anyway to minimize the impact of adding a type to my Android app with regard to the XML the user can provide and the Schema that validates it?
I found a solution. Using annotations and a framework like JAXB or Simple will let you serialize/deserialize objects to/from xml. JAXB can be run on Android which could be a solution. link
But JAXB is pretty heavy weight so I'm favoring Simple because it is more lightweight.

Line number information in Spring XML Namespace Handler

I'm writing custom Spring namespace handler (Java). If the XML is invalid, I'd like to report error message that will include line number (in the parsed document), so that user knows where to look. However, I don't know how to retrieve line number from DOM objects or otherwise.
Note that I'm talking about errors that are not discovered by XSD validation (those report line numbers correctly).
Is it even possible to get such information from inside Namespace handler?
Thanks,
Ondrej
If you are using the SAX parser you can extend the DefaultLocator class and register a Locator in setDocumentLocator method. The locator gets notified when an event occurs and therefore you can call getLineNumber() method to obtain the line number of interest.

Are there any 2-way template to object libraries?

We frequently use templates as a way of serializing an object. That is, given a Java POJO and a suitable XML template that includes placeholders like ${person.address.street}, we can output fully formed XML etc.
Are there any libraries where you can take that same template and a sample piece of output, and go the other way? That is, produce a populated Java bean (for instance) from a template, a sample XML document and, I guess, a Class name.
You can use Freemarker to help you it's a great JAVA API
You can have XML template file which call getters and then create a dynamic XML file.
Tell me if it's helps,

Categories

Resources