• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

complex XML doct & SAX parsing

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ajith,
Shivakumar here. I am quite new to XML. I went through the reply , you had
given to kmajith at Forum page on java technology & XML on 19th march . I am
facing with more or less a similar problem . The instance is as follows :
<X>
<Y>10</Y>
<Z>
<ZZ>20</ZZ>
<A>
<B>25</B>
</A>
</Z>
</X>
Now, I need to create an object of 'X' using java , which inturn conatins a
simple type, 'Y' and a complex type 'Z'. and the complex type 'Z' inturn
contains a simple type 'ZZ' and a complex type 'A'....and so.
Now while I am parsing this xml document using SAX parser, I need to create
an instance of the appropriate objects and contain them finally within the
ultimate super object 'X'. I am trying to solve tis, but i am not able to
succeed. Can you just suggest me as to how I should resolve this problem. I
couldn't understand the solution you had given to kmajith. Can you just
explain it in simple terms to me. Its quite urgent.
Thanks & regards,
shivakumar.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This kind of problem is much easier using a DOM parser rather than SAX. DOM automatically creates the tree structure.
However, it is possible in SAX. You need to keep a stack of objects as you are parsing. In the startElement method, you create the new object and connect it to its parent. In the endElement method, you pop it from the stack. The correct element is on the stack during the characters() method.
This only works when you are sure you have a valid document (i.e., you know that a Y is always nested within an X, etc.). Otherwise, you need defensive code to test your assumptions, and the whole thing gets even more complex.
Here is an example that will read the sample data you supplied (saved as "test.xml"), construct the tree of objects rooted in X, and dump the tree at end in the same format you started with.
 
shivakumar sekaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
many thanx for that Phil. thats very useful to me.
cheers
shiva
 
shivakumar sekaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Phil,
Thanx for your prompt reply.
I am struck up with this problem again. Actually, the application should be generic and there should not be any hardcoding in it. And in my case, i have an utility class, which makes use of the Bean introspection technique and identifies that the attributes of the parent object if it is either simple type, a complex type ,an array of either simple or complex type ..etc. If the XML document contains only simple types , then i could easily set the tag values using the user defined method setCompValue(Object rootObj,$ attribname,$ attribValue), 'coz i am considering all the tag elements in the document as objects, irrespective of whether they are simple or complex. While the utility introspector class encounters an complex object , i need to call the method again recursively, which tries to grab the attributes of that object inturn ... So if i am doing this setting of values within the methods of the DefaultHandler class, i cant do this , as i could not call the startElement() or endElement() method recursively.
could you kindly suggest me as to where i should place this piece of code , which actually would set the attributes in the rootObject.
thanx in advance
cheers
shiva
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, I would recommend you use the DOM interface rather than SAX for this task. You can navigate down the document tree from the root element, enumerating child nodes as you go. For each one, you could do introspection to get the constructor for a node accepting that element type. I only gave the SAX example because you said you were constrained to use SAX.
------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets
 
shivakumar sekaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx for your suggestion Phil. But my client insist on using a SAX parser instead of DOM. I can understand that it is pretty simpler to work out using DOM interfaces. I dont have an option in this regard. I am trying my best to arrive at a logic for this. Please help me, if you get some ideas on this.
Thanks & cheers
shiva
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shivakumar,
Have you looked at JDOM (at www.jdom.org)? This is a package that builds an internal copy of your XML data using Java Lists. It seems to me that this may just circumvent your problems on not being able to use a DOM parser.
Cheers,
Rob.
 
shivakumar sekaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
Thanx for your reply and the information, you have provided. JDOM is quite promising. But, anyhow, i have already arrived at a couple of API's which looks more or less similar to the API's existing in JDOM. To me, it looks that JDOM again would use some way of caching the objects in the memory, in some form or the other, as the API's seems to be. what do you think ? and that its still awaiting approval from Sun. So I dont think I could use it right away.
Should it be possible for me to look around the logic they have used for solving the "Collection" crisis ? as thats the only thing, i am yet to solve in my application .
Thanks once again.
cheers
shiva
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Shiva ,
The problem you have posted seems impossible to solve with
SAX or DOM. I suggest you contact Amitabh Nigam regarding
soln to this problem.
Bye,
Master.
 
Rob van Wees
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiva,
I'm unsure what you mean by 'collection crisis'. Could you elaborate a little?
Cheers,
Rob
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Master'
Your name does not comply with the JavaRanch naming policy. Please spare a moment and re-register with a name that meets the requirements. We will take care of deleting your current account.
Thanks!
Ajith
 
shivakumar sekaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah Rob,
consider the following document ( for collection's problem ):
<one>
<two>Two</two> // assume 'Two' to be a String data
<three>
<four>Four</four> // assume 'Four' as another String data
<flag>true</flag> // 'true' as a boolean value
<vect>5</vect> // here 'vect' is an instance of "Vector"
<vect>true</vect> // having an 'Integer','Boolean' & 'X'
<vect>X</vect> // where 'X' is an object reference of an
. // abstract class defined by me locally
. // here i need to decide after encountering
. // values of different types ( in this case
. // an Integer, Boolean & an Abstract
</three> // type,...etc ) for the same tag element ,
</one> // that its a collection of different objects
To sum up theres a class called 'one' which has two & three as its attribute. while two is of simple type, assume 'three' is of a type called Three ( an user-defined class )and three has its own attributes ,,.... etc
class one
{
String two;
Three three;
// assume there are respective setters & getters for them..
}
class Three
{
String four;
boolean flag;
Vector vect;
// assume this class has its own setters & getters
}

the actual problem , i would face in here is that in the 'vect' , i might not know , what exactly is the type of object contained as value... for instance i cant differentiate between an 'int' and 'Date' for both of them are represented as integer values ( date : in terms of milliseconds ). so i might not be able to construct objects accordingly , you see.
but , i think anyway, we would be avoiding this situation and make use of arrays of objects instead... so , we are ignoring it for a while..
and theres another problem, i am facing, while constructing an array of complex types here. consider the following situation :
<x> // level 0
<y>10</y> // level 1
<z> // level 1
<A> // level 2
<a>23</a> // level 3
<a>45</a> // level 3
</A>
<A> // level 2
<a>100</a> // level 3
<a>200</a> // level 3
</A>
</z>
</x>
here, i am trying to construct an ultimate object of type 'x'. which contains an object of simple type 'y' and an object of complex type 'z'. class 'z' contains an array object of type 'A' : array size is 2. and class 'A' has a simple array of type 'a', from the above scenario.
the sequence is this. i m trying to create the objects based on the level . now you could see that both 'A' is at level 2 ( as they are arrays ) and all the 'a' are at level 3. now when i create the first array element of type 'A', its getting created with an array of type 'a' , WHICH IS OF SIZE 4, just because they are at the same level ( 3 ) and their parents happens to be the same level as well... so its giving a problem , when the second ( array ) object of type 'A' is to be created... i dont know how this could be rectified.. hope i have explained myself clearly... see if can throw some light on this.. Rob.
thanx & cheers
shiva
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic