Java and SAX2

Discussion in 'Java' started by pp, Aug 2, 2005.

  1. pp

    pp Guest

    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?
     
    pp, Aug 2, 2005
    #1
    1. Advertising

  2. pp

    jan V Guest

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

    What does that mean "to protocol" ?
     
    jan V, Aug 2, 2005
    #2
    1. Advertising

  3. pp

    pp Guest

    Hi jan V,

    jan V wrote:
    > > then I need to protocol the content of the 'name' attribute and

    > 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?!
     
    pp, Aug 2, 2005
    #3
  4. pp

    jan V Guest

    > 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.
     
    jan V, Aug 2, 2005
    #4
  5. pp wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 2, 2005
    #5
  6. pp

    pp Guest

    Thank you, Jan! Thank you, Thomas!
    Very useful suggestions.
     
    pp, Aug 3, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Per Magnus L?vold
    Replies:
    0
    Views:
    2,077
    Per Magnus L?vold
    Nov 16, 2004
  2. Kenneth Porter

    Tutorials/examples for SAX2 with Perl

    Kenneth Porter, Dec 13, 2003, in forum: XML
    Replies:
    2
    Views:
    502
    Tad McClellan
    Dec 13, 2003
  3. Christian Roth

    Comments in SAX2

    Christian Roth, Jul 22, 2004, in forum: XML
    Replies:
    1
    Views:
    440
    Christian Roth
    Jul 22, 2004
  4. Jesse Thompson

    Perl + SAX2 = slow?

    Jesse Thompson, Sep 13, 2004, in forum: XML
    Replies:
    13
    Views:
    2,201
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Sep 15, 2004
  5. Replies:
    2
    Views:
    3,481
Loading...

Share This Page