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.
Related
I have an xml like shown. I want to convert this document into equivalent Java object shown below
<page>
<body>
<category id="category-0_2">
<container title="Spotlight" slug="spotlight"></container>
<container title="Just Added Shows" slug="just_added_shows"></container>
<container title="Originals" slug="originals"></container>
<container title="Popular TV Shows" slug="popular_tv_shows"></container>
<container title="Wonder Women" slug="wonder_women"></container>
</category>
</body>
</page>
public class HomeXML {
Page page;
Body body;
Category category;
List<Container> containerList;
}
Is it possible to do this using simpleframework xml serialiser? I can get the whole XML and parse it manually but I am looking for ways to do this via the simpleframework API.
Got the answer. simple framework provides utility functions which convert the XML document into Java object list.
More details here http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#util
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.
I'm trying to unmarshal an xml document that contains lists of composite elements. Is it possible to do this without having to set the aliases individually for the list and the composit element.
the xml I'm consuming looks something like this:
<Library>
<Books>
<Book>
<author>author1</author>
<title>title1</title>
</Book>
<Book>
<author>author2</author>
<title>title2</title>
</Book>
</Books>
<!-- other things here -->
</Library>
I have made the appropriate classes to correctly model the data
and using xstream
XStream stream = new XStream();
stream.alias("Library",Library.class);
stream.alias("Book",Book.class);
stream.alias("Books",List.class);
Library lib = stream.fromXML(xml);
I am trying to avoid having to alias book to book.class and books to list.class
is this even possible or do I need to find a workaround of some sort?
I have an XML, for example
<root>
<config x="xxx" y="yyyy" z="zzz" />
<properties>blah blah blah </properties>
<example>
<name>...</name>
<decr>...</descr>
</example>
<example>
<name>...</name>
<decr>...</descr>
</example>
</root>
and I need to get nodes config, and properties and all values in it.
Thank you
Xpath can fetch you the data in the config tag. You need to create an expression first like this
expression="//root/config/#x", to get value of x,y,z.
For properties, follow this thread :
Parsing XML with XPath in Java
Hope this helps
DOM,DOM4J,SAX..
if the size of XML file is small,you can use DOM or DOM4J,but the size is big , you use the SAX
If you directly want to query or fetch data XPath can help, but if you want the data as Java Objects so that you can use it further then use JAXB
You can use SAX parser to read the xml manipulate its event based parsing and consumes more memory.
If your xml is big and requires lot of manipulations then go-for DOM/DOM4j either is good. DOM4L is very latest. DOM is widely used in industry.
Based on your requirement go for good parser.
Thanks,
Pavan
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.