XML parsing in JAVA with JAXB Annotations - java

I need following Kind of XML in result from a JAVA class using JAXB annotation(like #XmlElement , #XmlAttribute etc.)
Following is the XML result that i want...
<?xml version="1.0" ?>
<ROOT>
<A>
<A1></A1>
<A2></A2>
</A>
<B>
<B1></B1>
<B2></B2>
</B>
</ROOT>
For that XML response what should be my Sample Java Class and what should be the annotations for that, keeping in mind that all the nodes should be case-sensitive ie. element B shouldn't b
Thanks in advance

Related

Creating dynamic XML

Input XML can change, say if the following XML is coming as input:
<Root>
<Fruits></Fruits>
<FruitsName>
<Apple></Apple>
<Mango></Mango>
</FruitsName>
</Root>
than output should be generated based on a mapping file.
Here say Fruits to be mapped with F1.
FruitsName to be mapped with FN.
Apple to be mapped with App.
Mango to be mapped with Man which will be present in properties file.
Output:
<Root>
<F1> </F1>
<FN>
<App></App>
<Man></Man>
</FN>
</Root>
Now if the input XML has different tags than XML should be generated based on
the mapping file/properties file.
You are doing an XML to XMl transformation.
You can Make use of XSLT to achieve the output.
Thanks!
You can achieve using following simple dataweave script:
%dw 1.0
%output application/xml
---
{
Root:{
F1: payload.Root.Fruits,
FN:{
App: payload.Root.FruitsName.Apple,
Man: payload.Root.FruitsName.Mango
}
}
}
output as you expect:
<?xml version='1.0' encoding='windows-1252'?>
<Root>
<F1></F1>
<FN>
<App></App>
<Man></Man>
</FN>
</Root>

JAXB marshal any xml to a String [Marshal mutiple tags at once to a String]

My requirement: I want to make a JAXB class for below xml
<Request>
<Data>mydata</Data>
<Details>
---Any XML content(utf-8)---
</Details>
</Request>
any xml content can be like B and so on
What I WANT
I want Any xml content in string object (HOW DO I DO THIS)(How do we marshal it to a single String object?)
Say Any Xml content is
<A>
<B>
value
</B>
</A>
I want it to bind to String object as <A><B>value<B><A>

How to unmarshal XML valid part of not xml file - JAXB

I have to marshal some class in XML format to the file (with no xml extension) as a header, and then append at the end of a file some invalid content. After that, obviously I would like to unmarshal this class with a JAXB unmarshaller, but I got a problem - it returns null, because the file is no longer valid xml (without additional content, all works perfect).
My file looks like:
<Root>
<Element>
...
</Element>
...
</Root>
[binary invalid content]
Is that possible to unmarshal only xml valid part of a file?
Thanks for all your help.

Is it possible to create an object from XML file using DOM?

I use DOM parser to read data from XML file. I know how to read, modify and write back the data. However, I would like to know if it is possible to create an object from an XML file.
I have an XML file which looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE people SYSTEM "validator.dtd">
<people>
<student>
<name>John</name>
<course>Computer Technology</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
<student>
<name>Foo</name>
<course>Industrial Electronics</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
</people>
and I would like to make it an objects out of it so I can pass them around. Does a solution exist ?
Yes. This is possible through JAXB (Java API for XML binding)
All JAXB implementations provide a tool called a binding compiler to bind a XML schema in order to generate the corresponding Java classes.
For details refer: http://www.oracle.com/technetwork/articles/javase/index-140168.html#xmp1
You could have a look at XML beans or JAXB libraries. In case you don't have a schema file but have a sample XML file, you could create one using inst2xsd tool of xmlbeans. http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html. This could get you started with the schema.

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