Java and SAX2

P

pp

I would like to process an XML document with a Java application,
utilizing the Apache Xerces SAX2 parser.

Based on the name of an element and the value of its specific
attributes, I would like to perform some processing on the data that
this element encloses. Suppose, for example, an element in the XML
document looks like

<my_element name="el" number="1"> element data </my_element>

then I need to protocol the content of the 'name' attribute and based
on its value, I need to process the 'element data' contents. What
disturbs me is that I cannot make all the processing at once and should
wait for the specific parser callbacks to provide the needed
information. E.g. I obtain the 'name' attribute value in the
startElement callback and the 'element data' in the characters callback
of the SAX ContentHandler interface.

Is there a finer way to do the processing in a single method, instead
of using global variables that should be changed by the respective
callback method? I cannot rely on some return parameters for the SAX
callbacks, because their return type is void.

I do not want to have a random access over the whole information in the
XML document. It is about having the value of the 'name' attribute and
the 'element data' of a single 'my_element' at the same time. Any ideas
how to make that happen please?
 
J

jan V

then I need to protocol the content of the 'name' attribute and

What does that mean "to protocol" ?
 
P

pp

Hi jan V,

jan said:
What does that mean "to protocol" ?

Sorry for not being clear enough in my explanations. I've simply meant:
get the value of the 'name' attribute, get the 'element data' and
(based on what the value of the 'name' attribute is) process the
'element data' in one way or the other. So "to protocol" implies that I
need the value for 'name' and the 'element data' contents at the same
time, in order to be able to decide how the 'element data' is further
handled. Hope it's clear now.

Any suggestions how to do it please?!
 
J

jan V

Any suggestions how to do it please?!

I've used SAX once before, and the only way I can see it working is for you
to build up, field by field, an object representing each node, and when that
object is fully formed, you simply proceed by querying the object. Unless
I've missed the point of SAX-style XML processing, I think that's the way
it's meant to be done.

Note that there are frameworks which automate this process of turning XML
into Java objects. Apache have one such framework, I think it's called
Digester or something like that.
 
T

Thomas Hawtin

pp said:
I would like to process an XML document with a Java application,
utilizing the Apache Xerces SAX2 parser.

Based on the name of an element and the value of its specific
attributes, I would like to perform some processing on the data that
this element encloses. Suppose, for example, an element in the XML
document looks like

<my_element name="el" number="1"> element data </my_element>

then I need to protocol the content of the 'name' attribute and based
on its value, I need to process the 'element data' contents. What
disturbs me is that I cannot make all the processing at once and should
wait for the specific parser callbacks to provide the needed
information. E.g. I obtain the 'name' attribute value in the
startElement callback and the 'element data' in the characters callback
of the SAX ContentHandler interface.

In the startElement method stash the name and number in member variables.

In the characters method append to a StringBuilder.

In the endElement method do your processing.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top