Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML to Object[] Utility

 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to XML.

I have to parse following XML code...

<task>
<export>
<title>List of Table</title>
<filename>TableList.csv</filename>
<query> select * from tab</query>
</export>

<export>
<title>List of Employee</title>
<filename>EmployeeList.csv</filename>
<query> select * from Employee</query>
</export>
</task>

I need above data in Object[], where each value of Object[] contains another Object[], that hold values of title, filename, query.

Can anybody provide me with the same code or utility?

Reg,
Chetan
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Castor tool is probably what you want to look into for this.

Link1
Link2

- m
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am the author of the FormattedDataSet API. Primarily the API is used to get tabular data (result sets, collections, and Arrays) into text (xml, html, form letters etc). In working with tabular data I realized that most data is easy to get into an array (Object[][]), so over time I have come up with utilities that help manipulate arrays.

The link below is not in my api as it still needs some work, however it is pretty straightforward. Here is the link:

http://cvs.sourceforge.net/viewcvs.py/fdsapi/fdsapi/Examples/HTTPUtils/XMLToArrayConverter.java?rev=1.3&view=auto

Below is usage code, which is similar to the code in the main method in the above class. Feel free to take the class code and modify it to your needs.

 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have developed following class for the same...



Create 1.xml file with following text in c:\.

[code]

<?xml version="1.0" encoding="UTF-8"?>

<FileList>
<file>
<filename>First</filename>
<title>First File</title>
<desc>1</desc>
<query>select * from tab</query>
</file>

<file>
<filename>2</filename>
<title>2</title>
<desc>2</desc>
<query>select * from tab2</query>
</file>
<file>
<filename>2342342</filename>
<title>3242</title>
<desc>drert</desc>
<query>erter6786</query>
</file>
</FileList>
[code]

Kindly give me feedback.

Reg,
Chetan
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Parekh:
Hello I have developed following class for the same...
Kindly give me feedback.

Reg,
Chetan

Feedback on what?? You mean on your java code ??
Thanks
Balaji
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About logic and code.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more flexible solution would be to pass the parse method the xml as a String, and have separate code read in the file and put it in String format. I am not sure how big your xml files are. Obviously if they are huge then you may not be able to read them in as a String.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!!

This is a good idea, specifically if you are accessing file from different server or you save xml in database or you are getting xml from other application via messaging service.

I will implement this thing.

Warm Regards,
Chetan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic