Parse XML into Java Object List using simpleframework - java

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

Related

Process single element array when converting XML to JSON (<?xml-multiple?>)

I'm using org.json.XML.toJSONObject() method to convert an XML string to JSON. Here is a sample XML string that I need to convert.
<?xml version="1.0" encoding="UTF-8"?>
<jsonObject>
<data>
<?xml-multiple accounts?>
<accounts>
<Id>123</Id>
<creationDate>2021-10-21T15:43:00.12345Z</creationDate>
<displayName>account_x</displayName>
</accounts>
</data>
<links>
<self>self</self>
<first>first</first>
<prev>prev</prev>
<next>next</next>
<last>last</last>
</links>
<meta>
<totalRecords>10</totalRecords>
<totalPages>10</totalPages>
</meta>
</jsonObject>
Here, 'accounts' is an element of an array and contains only a single element. But the org.json library cannot detect this. It can detect only if there are multiple elements.
My question is, is there a library that I can use to detect a single element array using the available tag in the XML string?

Best way to bind a XML File in Java (NetBeans)

i want to bind this simple XML File in my java project:
<?xml version="1.0" encoding="UTF-8"?>
<itg>
<reader>
<chapter id="1">
<subchapter id="1"></subchapter>
<subchapter id="2"></subchapter>
</chapter>
<chapter id="2">
<subchapter id="1"></subchapter>
<subchapter id="2"></subchapter>
</chapter>
<chapter id="3"></chapter>
</reader>
<questions>
</questions>
</itg>
I use NetBeans, and actually i bind the XML File by parsing the xml file into a ArrayList, an bind the list.
It works, but it is possible to bind the xml File in a better way?
Thanks!
For this small XML (and not only) I would recommend that you take a look at JAXB. The two basic operations are marshalling (converting Java objects to XML data) and unmarshalling (converting XML data to Java objects) but verification and so on is also provided.

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 using Simplexml

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.

XML highcharts file creation using java

the question is how to create this xml file exactly for me to be able to use it in the get function of highcharts? The CSV file is created rapidly but now i want to create the xml file from the back end which is java.
<chart>
<categories>
<item>..</item>
</categories>
<series>
<name>xxx</name>
<data>
<point>1</point>
<point>2</point>
<point>3</point>
<point>4</point>
</data>
</series>
</chart>
Upon request:
<chart>
<categories>
<item>Apples</item> these two items must be populated from the database
<item>Pears</item>
</categories>
<series>
<name>John</name> this one also from database
<data>
<point>8</point> this is the data part, retrieved from database
<point>4</point>
<point>6</point>
<point>5</point>
</data>
</series>
</chart>
I want to create this type of xml structure using a java method or class where i retrieve all the information i need and then pass in into the xml file.
Anyways, am using xml to populate data for highcharts now.. Thanks for the answer

Categories

Resources