generate java class using Simplexml - java

I have an xml with the following structure:
<Items>
<item>
<IntItem>
<value>1</value>
</IntItem>
</item>
<item>
<BoolItem>
<value>true</value>
</BoolItem>
</item>
<item>
<StrItem>
<value>word</value>
</StrItem>
</item>
</Items>
It is list of items, that can be different types (bool, int, string). Could you help me write java class with annotation for above xml structure?

Are you wanting to actually generate the 'java class' with an xml file? (That is what the title says)
If so, then you should look into some modeling frameworks that allow you to generate code from xml files (models). Acceleo and EMF are just a couple. But there are more.
BUT, if you are just wanting to populate fields in your class with an xml file... look into some xml parsers for android and parse the xml and just assign the values to the fields. SAX and DOM are two very popular parsers for Android.

Related

Parse XML in which tag name is not fixed

It is easy to parse XML in which tags name are fixed. In XStream, we can simply use #XStreamAlias("tagname") annotation. But how to parse XML in which tag name is not fixed. Suppose I have following XML :
<result>
<result1>
<fixed1> ... </fixed1>
<fixed2> ... </fixed2>
</result1>
<result2>
<item>
<America>
<name> America </name>
<language> English </language>
</America>
</item>
<item>
<Spain>
<name> Spain </name>
<language> Spanish </language>
</Spain>
</item>
</result2>
</result>
Tag names America and Spain are not fixed and sometimes I may get other tag names like Germany, India, etc.
How to define pojo for tag result2 in such case? Is there a way to tell XStream to accept anything as alias name if tag name is not known before-hand?
if it is ok for you to get the tag from inside the tag itself (field 'name'), using Xpath, you can do:
//result2/*/name/text()
another option could be to use the whole element, like:
//result2/*
or also:
//result2/*/name()
Some technologies (specifically, data binding approaches) are optimized for handling XML whose structure is known at compile time. Others (like DOM and other DOM-like tree models - JDOM, XOM etc) are designed for handling XML whose structure is not known in advance. Use the tool for the job.
XSLT and XQuery try to blend both. In their schema-aware form, they can take advantage of static structure information when it is available. But more usually they are run in "untyped" mode, where there is no a-priori knowledge of element names or structure, and everything is handled as it comes. The XSLT rule-based processing paradigm is particularly well suited to "semi-structured" XML whose content is unpredictable or variable.

parsing of an xml which having inner tags changing dynamically

I need help in parsing one xml. Though I know xml parsing very well. but I am stuck with parsing of one particular xml which having it's inner tags changing dynamically. That xml I am getting in respoonse of some API. Sample xml is as follows:
<addon_brunch_data is_array="true">
<item>
<Monsoon_Magic_Lunch_Buffet__Adult_>
<currency_code>INR</currency_code>
<pid>575</pid>
</Monsoon_Magic_Lunch_Buffet__Adult_>
</item>
<item>
<Theme_Park_Lunch>
<currency_code>INR</currency_code>
<pid>583</pid>
</Theme_Park_Lunch>
</item>
<item>
<All_Restaurants_except_Imagica_capital>
<currency_code>INR</currency_code>
<pid>553</pid>
</All_Restaurants_except_Imagica_capital>
</item>
</addon_brunch_data>
In this example, I am receiving the fixed tag <addon_brunch_data is_array="true"> which is having an arraylist of <item>.
But the problem I am facing is,these inner tags of are not fixed :
<Monsoon_Magic_Lunch_Buffet__Adult_>
<Theme_Park_Lunch>
<All_Restaurants_except_Imagica_capital>
Sometimes I am getting the inner tags of as listed above, but sometimes the response changes as :
<addon_brunch_data is_array="true">
<item>
<Great_Indian_Food_Festival___Adult>
<currency_code>INR</currency_code>
<pid>575</pid>
</Great_Indian_Food_Festival___Adult>
</item>
<item>
<Great_Indian_Food_Festival___Child>
<currency_code>INR</currency_code>
<pid>583</pid>
</Great_Indian_Food_Festival___Child>
</item>
<item>
<Healthy_Meal_Pack>
<currency_code>INR</currency_code>
<pid>553</pid>
</Healthy_Meal_Pack>
</item>
</addon_brunch_data>
I need to write pojos for this, but I have not come earlier to parse such xml which is having inner tags changing dynamically like in above example I tried to give.
Guys Please help on this.

XML Parent & child Attributes reading in java?

I have the following data in my XML file.
<main>
<Team Name="Development" ID="10">
<Emp Source="Business" Total="130" Active="123" New="12" />
<Emp Source="Business" Total="131" Active="124" New="13" />
</Team>
<Team Name="Testing" ID="10">
<Emp Source="Business" Total="133" Active="125" New="14" />
</Team>
</main>
I want to read above data & store values into arrays,Can any one help on these?
Not sure why you need to convert those xml into Arrays, anyhow you can read xml and parse it by several ways. Normally we use DOM or Stax Parser and a Tutorial link is here, also here is a Java SAX Parsing Example tutorial.
Hope this can help you to achieve your goal. Update your question again if you stuck anywhere.
You can use parser in JAVA to parse the XML document. The package in java for this purpose is javax.xml.parsers . DocumentBuilder parses XML into a Document and Document is a tree structured data structure that is DOM(Document Object Model) readable file. Its nodes can be traversed/ changed/ accessed by DOM methods.
Here is a very good tutorial on XML DOM: http://www.roseindia.net/xml/dom/
and more specifically: http://www.roseindia.net/xml/dom/accessing-xml-file-java.shtml
also you can always refer to w3school for more theory on DOM!

Parsing XML using Digester

I have a xml and it has a list tag with number of items.
Example:
<param>
<id>12345</id>
<date>2012/07/10</date>
<list>
<item>
<name>Test1</name>
<code>C1</code>
</item>
<item>
<name>Test2</name>
<code>C2</code>
</item>
</list>
</param>
I want to map this in to Java a object using Jakarta Digester framework.
My plan is this.
Create a main ResponseDTO
Create a ItemDTO
Add a ItemDTO Array List to ResponseDTO
So after parsing this xml to Jakarta Digester engine I'm expectibg a ResponseDTO with ItemDTO list which are having real values.
Could some one kindly let me know how I can do this using Digester framework.
Have a look to this article: http://www.devx.com/Java/Article/21832
But there are dozen of XML de/serialization frameworks that are much more user friendly... (http://x-stream.github.io/tutorial.html#init).
M.

Generate java class hierarchy from xml (plist)

Are there any tools that can automatically generate java class hierarchy from xml (plist)?
Say we have:
<blah>
<item />
<item />
</blah>
And we need to get something like:
class Blah {
Collection<Item> items;
}
...and so on and so forth
If I get it right, then the elements shall be transformed into class and field names.
This can be done with a few lines of code:
parse the xml document into a DOM
walk through the DOM tree and create a java source file in memory (StringBuilder)
write the source file to your file system
Or use XSLT to create a transformation from your xml document to a java source file.

Categories

Resources