JAXB Does not Marshall as Expected - java

A buddy of mine asked me to post this question:
EDIT: He decided to post the question on his own here: JAXB Unmarshalls XML Incorrectly I tried to delete this question but couldn't.
I am trying to marshall a java class, called MyContainer. This class extends my another class, called Container. Container has a variable called: protected List<ResourceReference<Component>> child
The only variable MyContainer has is the one it inherits from Container. This List, however, will not marshall with JAXB when I have child listed as an XmlList.
It will marshall as an XmlElement, however this is not how I want the data displayed. When it is marshalled as an XmlElement, it lists the attributes in the List, but not 'heirarchially' It just lists the two elements as if they were properties of the class, as opposed to belonging to the child list.
How do I get Jaxb to marshall the List (Which is instantiated as an ArrayList) correctly?
EDIT: Here's an example:
This is what I am getting:
<MyContainer name="Container Name">
<booleanVal>false</booleanVal>
<child id="Test Comp 1"/>
<child id="Test Comp 2"/>
</and>
This is more what I expect:
<MyContainer name="Container Name">
<booleanVal>false</booleanVal>
<child> /// This would be the class variable of the extended Class, Container
<ResourceRef id="Entry 1">
<ResourceRef id="Entry 2">
</child>
</and>
Admittly, my current results is probably from the List in Container being marked with the #XmlElement annotation. However, JAXB will not marshall anything else..

Annotate your list with #XmlElementWrapper. See javadoc.

Related

Jackson xml mapper wraps generic list elements into a listname extra tag

I'm stuck on a wrong XML serialization I'm obtaining with Jackson XML mapper serializer.
I have a generic RESULT TAG that must contain some other elements, and these elements may be lists of elements, something like this:
<RESULT>
<THINGS>
<THING>
<SOMETHING_ABOUT_THING>001</SOMETHING_ABOUT_THING>
</THING>
<THING>
<SOMETHING_ABOUT_THING>002</SOMETHING_ABOUT_THING>
</THING>
</THINGS>
</RESULT>
But what I'm obtaining through standard XML mapper is something with an extra tag like this:
<RESULT>
<THINGS>
<THING>
<SOMETHING_ABOUT_THING>001</SOMETHING_ABOUT_THING>
</THING>
</THINGS>
<THINGS>
<THING>
<SOMETHING_ABOUT_THING>002</SOMETHING_ABOUT_THING>
</THING>
</THINGS>
</RESULT>
I do not have any custom object, because tag names and tag content are dynamically retrieved from a database, I'm just working with an HashMap, and trying to serialize it. The hashmap holds a single THINGS key that contains as value a list of maps, and each map contains a single THING element.
What can I do to achieve the first result?
Thanks in advance

XPaths using EclipseLink MOXy and JAXB given a value of another element

I want to get the text value of a certain element in xml. In the XML below i want to get the value of SUBCHILD when the value of CODE is 'Code1' irrespective of the position of the MP entity when there could be many MP elements. Also i want to be able to do this using JAXB and MOXy with the #XMLPath attribute
The xml i have is this:
<RQ>
<PQ>
<MP>
<INFO>
<CODE>Code1</CODE>
</INFO>
<CHILD>
<SUBCHILD>VALUE for Code1</SUBCHILD>
</CHILD>
</MP>
<MP>
<INFO>
<CODE>Code2</CODE>
</INFO>
<CHILD>
<SUBCHILD>VALUE for Code2</SUBCHILD>
</CHILD>
</MP>
</PQ>
</RQ>
I want 'VALUE for Code1' irrespecive of its position, the MP element containing Code1 could be anywhere. The XPath i would use for this would be:
RQ/PQ/MP[INFO/CODE='Code1']/CHILD/SUBCHILD
but i cant seem to get the value i want from MOXy, is this functionality not supported, I know that you can map based on attributes, but i need it depending on the value of another element
Any help would be appreciated
MOXy currently does not support XPaths of the following form on its #XmlPath annotation.
There is an open bug to have an exception thrown if the XPath specified is not supported.
https://bugs.eclipse.org/397101
Can you open an enhancement request for the behaviour you are looking for?

How to remove an specific xml attribute from org.w3c.dom.Document

I have this XML:
<Body xmlns:wsu="http://mynamespace">
<Ticket xmlns="http://othernamespace">
<Customer xlmns="">Robert</Customer>
<Products xmlns="">
<Product>a product</>
</Products>
</Ticket>
<Delivered xmlns="" />
<Payment xlmns="">cash</Payment>
</Body>
I am using Java to read it as a DOM document. I want remove the empty namespace attributes (i.e., xmlns=""). Is there any way to do that?
You need to understand that xmlns is a very special attribute. Basically, the xmlns="" is so that your Customer element is in the "unnamed" namespace, rather than the http://othernamespace namespace (and likewise for other elements which would otherwise inherit a default namespace from their ancestors).
If you want to get rid of the xmlns="", you basically need to put the elements into the appropriate namespace - so it's changing the element name. I don't think the W3C API lets you change the name of an element - you may well need to create a new element with the appropriate namespaced-name, and copy the content. Or if you're responsible for creating the document to start with, just use the right namespace.

Building different XML files with same JAXB objects

Let's say I want to use JAXB to generate two XML files -- one containing a list of items, the other containing detailed definitions of those items. For instance, something like this:
Garage.xml:
<garage>
<car ref="1" />
<car ref="2" />
</garage>
Cars.xml:
<cars>
<car id="1" color="blue" model="Impreza" />
<car id="2" color="plaid" model="Taurus" />
</cars>
Is there anything clever I can do to define a single JAXB Car.java object that will allow me to use the same object for both files? If not, is there an accepted best practice anyone would recommend beyond the obvious solution of creating two separate Car classes? (One with the ref attribute, the other with the id, color, and model attributes.)

JAXB facilities in unmarshaling

Hello guys how can i unmarshall list of objects in XML. My XML will be like this
<messageContainer class="class1">
<a>
<b />
</a>
<MessageB />
</messageContainer>
<messageContainer class="class2">
<a>
<b />
</a>
<MessageB />
</messageContainer>
And i want to get list of object in the end.
Any Valid XML file can contain only one XML Root Element.
I assume that u have the above list as a child of an RootElement(say ).
Then your annotated RootElement class should look like this.
#XmlRootElement(name = "RootElement")
public class RootElement{
#XmlElement(name = "messageContainer", required = true)
private List<messageContainer> containerList;
}
Where MessageContainer is a class by itself.
Note : The more simpler way to generate these binding class is, write XSD(or generate using online tools) for your XML and compile those XSD with XJC compiler.

Categories

Resources