Solved

Java XML SAX

Posted on 2004-08-04
13
336 Views
Last Modified: 2013-11-23
hi experts,

i have a question regarding SAX.. hope is the right place to ask.

im quite new in xml processing, i am able to read and process xml through SAX, and store them into classes. now i would like to know how to revert this process?

i have the xml schema, anyone knows how to transform classes/database data back into XML format through SAX?

i search quite anumber of sites, and couldnt find a simple solution i can understand..


thnz
0
Comment
Question by:mooy
13 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11723872
You need to put all of the XML into a StringBuffer and that's all.

StringBuffer sb = new StringBuffer();
sb.append("<xml...>");
sb.append("<...>");
and so on.
0
 

Author Comment

by:mooy
ID: 11724208
is there a more convient way?
bcos my xml file is rather complex.

can i use the xml schema to phrase in data and have and XML output? using SAX transformation..
0
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 11724228
SAX is sequential which means that it reads the XML file sequentially and then just fires events when certain elements appear. You need to store the data somewhere yourself in order to use them and therefore you will need to use this data to reconstruct the XML file.

It would be easier to reconstruct the XML file if you were using DOM.

For sax transformations have a look here: http://javaalmanac.com/egs/javax.xml.transform.sax/Dom2Sax.html
0
 

Author Comment

by:mooy
ID: 11732269
yup.. i have store this data in to memory as classes, eventually a database.

how to get the data from database and then generate a XML file?

the link is to read from a XML file, i can read from a XML but now the problem is how to generate a new XML file after manupulating the values in the file.?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11733357
Since you already have the data into memory as classes then use what I described in my first comment (a StringBuffer). You have only the data into memory not the structure of the XML file, you have to devise a way to structure the XML file.

There might be other ways that I am not aware of, but you might want to take a look here: http://www.ibiblio.org/xml/books/xmljava/ maybe you will get some ideas.

> how to get the data from database and then generate a XML file?

HOw do you save it in the database? As a BLOB/CLOB?
0
 

Author Comment

by:mooy
ID: 11770824
after my research i found various way in generating out a XML file,

my problem here is getting domain classes values and putting them into XML format, through XSL.

-SAX , setting the content handler (one by one) Can i parse in my XSL and the output format will be like my stylesheet?
-JAXB, it creates alot of classes with the xjc.
or is there other ways in getting round this?

May i know which is a more efficient way of doing it? (my program will be running on lowend machines eg.win98)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:girionis
ID: 11770863
I am not sure about XSL, it's been a long time ago since I last used xalan. But I really do not see the need to process the XML again, you already have the data represented as java objects.
0
 

Author Comment

by:mooy
ID: 11771318
hmm...excatly

now i need to transform the java objects back into XML using the same XSL format. how im i going to do it?

thnx girionis, you teaching a noob here!! :o)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11771336
Where do you keep these objects? Do you store them in a collection like a Vector or an ArrayList?
0
 

Author Comment

by:mooy
ID: 11791352
hmm.. i store them in ArrayList.. is a huge structure..

i found a long tedious way in generating XML file out...it uses SAX transformation to serialize data into XML elements...

is there other ways?
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 75 total points
ID: 11880071
You might be able to use the XMLEncoder to serialize your ArrayList into an XML document, then XSLT to transform this document into the format you require...

http://java.sun.com/products/jfc/tsc/articles/persistence4/

Otherwise, I think it's the long tedious way :-(

0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 75 total points
ID: 11888075
Try one of these java libraries.  They allow you to output well formed XML documents into an output stream:
http://xmlenc.sourceforge.net - I have used this one and it works well
http://swan.sourceforge.net/
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get weblogic logged in user in java 2 57
Java Jpanels and Jframe 8 28
Windows Tool to Build Android and iOS App 3 51
map related example 6 30
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now