XML without root element in JAXB - java

I was wondering whether there is a way to create an object such that a list of such object does not need a root element. For example, if I wanted to create an XML like
<Dogs>
<Dog>A</Dog>
<Dog>B</Dog>
<Dog>C</Dog>
</Dogs>
I could have the class Dogs which would be the root element and has a List<Dog>. Now supposed I want to get rid of the encapsulating element <Dogs>. So that the list of dog would look like
<Dog>A</Dog>
<Dog>B</Dog>
<Dog>C</Dog>
how should I construct my classes?

In XML this is not possible. The specification at http://www.w3.org/TR/xml/#NT-document clearly says that a document has one root element.
Your second XML-like code is therefore not an XML document, but a concatenation of three XML documents. But parsers aren't usually prepared for this kind of input.

Related

XML UnMarshalling based on child nodes

I recently got an old XML over HTTP API. It has few response types and all those responses have no namespace or type attributes. They all have the same root node and then different set of child nodes.
Is there a way in java to UnMarshall such XMLs ? It would be like using child nodes as discriminator fields. Two sample responses are given below.
<Response>
<A1/>
<A2/>
</Response>
<Response>
<B1/>
<B2/>
</Response>
The best approach really depends on what you want to do. If you just want to unmarshal the data you could define a model using JAXB, for instance, which includes all the potential child elements. Then when you unmarshalled an instance documents only the child elements actually present in the document would have values.
If you instead want to have separate models for the different response variations your best approach would be to use a BufferedInputStream and call mark() at the start, then read enough of the document with a pull parser such as XMLStreamReader to determine the actual response type. Then you can reset() the stream to the start of the document and start over using JAXB with the appropriate data model.

how to skip few element validations in xsd in java

I am trying to validate an xml against an xsd in java.
In XSD one of the field(tax_id) is defined as manadatory element.
But in my scenario I pass an xml to another component, that component fills the manadatory
field(tax_id).
Before sending that xml to the next component, I have to validate that xml against the xsd.
As, in that XSD element tax_id is defined as mandatory element, I get exception for not filling mandatory element (tax_id).
I can create a new xsd by making tax_id as optional field, but with this we would be having 2 xsds.
Is there any way to skip/ignore few elements while validating in java?
In general, no. The purpose of the XSD is to specify the rules that the document must meet, in order to be valid. You can't ignore or skip some of those rules. If you could, you'd probably have other problems. For example, if required elements were really optional (or could be) then technically any element that was supposed to contain a bunch of other elements could be empty (and still valid) under that more lax validation.
In your situation, you probably have two options:
Change your workflow - make sure the first component populates the XML with the empty tax_id. Then it will validate.
Introduce a second schema - one earlier in the "pipeline" of processing, that doesn't require tax_id. Then validate against that.

Parse xml with same properties but different parent elements using JAXB

I'm using JAXB to parse some xml provided by a separate service. They provide this xml as different elements but I treat them the same. I.E.
<ElementA>
<name>foo</name>
<type>bar</type>
</ElementA>
<ElementB>
<name>foo1</name>
<type>bar1</type>
</ElementB>
This is how in the data but when I parse/unmarshall it with JAXB I want it to know that ElementA and ElementB are both just instances of the Element class and unmarshall it as such. Currently I am doing this by using the XmlRegistry to say that ElementA is an Element and ElementB is an Element.
However at some point we might receive ElementC and ElementD so I would like a way for JAXB to know those are also just Element without having to add an entry to the XmlRegistry everytime.

How to tell XStream to ignore child elements that have the 'class' attribute

I have some persisted XML that was generated by XStream, and looks like:
<CalculationDefinition>
<id>47</id>
<version>3</version>
<name>RHO error (pts)</name>
<expression class="com.us.provider.expression.AbsoluteValue">
....
</expression>
</CalculationDefinition>
I want to persist this content differently now, and want to tell XStream to simply ignore the expression element entirely. There's many links around that talk about how to do this with a MapperWrapper (eg XStream JIRA) but as far I can tell it doesn't work for an element that has a 'class' attribute.
This can be worked around by leaving an 'expression' field in the CalculationDefinition, but I'd rather not have to keep it there now that it's not used in code.
You could filter the incoming XML using XSL and removing the expression nodes, before passing it to XStream.

find by org.w3c.dom.Element object as parameter on org.dom4j.Document (convert org.w3c.dom.Element to org.dom4j.Element)

I have a org.w3c.dom.Document parsed by org.dom4j.io.DOMReader.
I want to search the dom4j DOM document via org.w3c.dom.Element.
say, I have a org.w3c.dom.Element that I need to locate on the dom4j document. I don't have any element information except for having the org.w3c.dom.Element object as a parameter.
I want something like dom4j doc.findByDomElement(org.w3c.dom.Element element) which would return a org.dom4j.Element.
What you need isn't provided by dom4j out of the box.
Dom4j allows you to parse an org.w3c.dom.Document as an org.dom4j.Document, but then you cannot search through the dom4j Document by an org.w3c.dom.Element; you should do that on your own method. For instance, you can use xpath to search some nodes. Also, dom4j provides org.dom4j.dom.DOMNodeHelper class which is a collection of utility methods to do some conversion from org.w3c.dom objects to org.dom4j objects but I've not found the method you need. Take a look here. In addition, dom4j provides the org.dom4j.io.DOMWriter class to do the opposite of org.dom4j.io.DOMReader.
When you say:
I don't have any element information
except for having the
org.w3c.dom.Element object as a
parameter.
Well, I think the Element object contains all the informations that you need to search manually through the dom4j tree.
Finally, I'd like to suggest you to use only one library to handle xml in your code. Do you have any particular needs? Why are you using both org.dom4j.Document and org.w3c.dom.Document?

Categories