You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Vinay Kakade <vi...@veritas.com> on 2001/12/19 12:48:27 UTC

How to get attribute name in characters() method?

Hi,

I have just started learning about Xerces C++ parser. I hope my question is appropriate for this mailing list.

My question is: In the characters() method of HandlerBase class, can we get the name of the enclosing attribute in which in which this character data appear?

For example:

<tag1>
    <tag2>someData</tag2>
</tag1>

In this xml file, when characters() method is called when "someData" is encountered, I want to retrieve name of enclosing attribute i.e. "tag2" in this case.

Please let me know if there is any pre-implemented method to do this. I serached the documentation but could not find any.

Thanks in Advance,
-Vinay Kakade.
    

RE: How to get attribute name in characters() method?

Posted by Erik Rydgren <er...@mandarinen.se>.
Sent before I thought that one through. The last STILL OPEN start element
call that is.
You will have to maintain a stack of open elements.
In startElement(), stick the name on top of the stack.
In endElement(), remove the topmost element from the stack.
in characters(), the data belongs to the topmost element on stack.

Now my ramblings might even make sense to anyone :)

/ Erik
  -----Original Message-----
  From: Erik Rydgren [mailto:erik.rydgren@mandarinen.se]
  Sent: den 19 december 2001 14:06
  To: xerces-c-dev@xml.apache.org
  Subject: RE: How to get attribute name in characters() method?


  Well the character data belongs to the last startElement call. Just store
the name then.

  / Erik
    -----Original Message-----
    From: Vinay Kakade [mailto:vinay@veritas.com]
    Sent: den 19 december 2001 12:48
    To: xerces-c-dev@xml.apache.org
    Subject: How to get attribute name in characters() method?


    Hi,

    I have just started learning about Xerces C++ parser. I hope my question
is appropriate for this mailing list.

    My question is: In the characters() method of HandlerBase class, can we
get the name of the enclosing attribute in which in which this character
data appear?

    For example:

    <tag1>
        <tag2>someData</tag2>
    </tag1>

    In this xml file, when characters() method is called when "someData" is
encountered, I want to retrieve name of enclosing attribute i.e. "tag2" in
this case.

    Please let me know if there is any pre-implemented method to do this. I
serached the documentation but could not find any.

    Thanks in Advance,
    -Vinay Kakade.


RE: How to get attribute name in characters() method?

Posted by Erik Rydgren <er...@mandarinen.se>.
Well the character data belongs to the last startElement call. Just store
the name then.

/ Erik
  -----Original Message-----
  From: Vinay Kakade [mailto:vinay@veritas.com]
  Sent: den 19 december 2001 12:48
  To: xerces-c-dev@xml.apache.org
  Subject: How to get attribute name in characters() method?


  Hi,

  I have just started learning about Xerces C++ parser. I hope my question
is appropriate for this mailing list.

  My question is: In the characters() method of HandlerBase class, can we
get the name of the enclosing attribute in which in which this character
data appear?

  For example:

  <tag1>
      <tag2>someData</tag2>
  </tag1>

  In this xml file, when characters() method is called when "someData" is
encountered, I want to retrieve name of enclosing attribute i.e. "tag2" in
this case.

  Please let me know if there is any pre-implemented method to do this. I
serached the documentation but could not find any.

  Thanks in Advance,
  -Vinay Kakade.


Re: How to get attribute name in characters() method?

Posted by Tuan Hoang <tu...@hoang.homeip.net>.
Check your handler for ignorableWhitespace().  Are you calling
a common method to handle the characters from both of them?

I believe it is good practice to keep appending the chars from
successive calls to characters().  The parser may have internal 
buffering so that the element value may be broken up into 
multiple calls to characters().  I think I read this in one
of my XML books.  Anyway, you really don't know when the element's
value is complete until you receive an endElement() which has
the element's name in there too.

Tuan

PS What parser version are you running?  I've used 1.5.2 and 1.6.0.

On Thu, 20 Dec 2001, Vinay Kakade wrote:

> Hi,
> 
> Thanks a lot for the reply.
> 
> In your reply, in the sample scenario, you have shown characters() method
> called many times. But, I think the scenario is like this:
> 
> startElement( "tag1", ...)
> characters("\n", 1)
> startElement( "tag2", ...)
> characters("\n", 1)
> characters( "someData", 8 )
> endElement( "tag2" )
> characters("\n", 1)
> endElement( "tag1" )
> characters("\n", 1)
> 
> At least this is what the output of my program suggest. I am using SAX2
> parser.
> 
> Please let me know:
> 1. How to avaoid the characters() method to be called for data that I want
> to ignore, such as "\n" corresponding to a newline after start of a tag and
> a newline after end of a tag?
> 2. For all the characters within "<tag2>" and "</tag2>", is the characters
> method called only once, or the parser may call it more than once?
> 
> Thanks once again,
> -Vinay Kakade.
> 
> ----- Original Message -----
> From: "Tuan Hoang" <tu...@hoang.homeip.net>
> To: "Vinay Kakade" <vi...@veritas.com>
> Cc: <xe...@xml.apache.org>
> Sent: Thursday, December 20, 2001 10:32 AM
> Subject: Re: How to get attribute name in characters() method?
> 
> 
> > Using the SAX parser, you could get something similar to this:
> >
> > startElement( "tag1", ...)
> > startElement( "tag2", ...)
> > characters( "so", 2 )
> > characters( "me", 2 )
> > characters( "Dat", 3 )
> > characters( "a", 1 )
> > endElement( "tag2" )
> > endElement( "tag1" )
> >
> > Basically in your handler for characters(), keep adding to a string
> > object (I use a class data member).  When endElement() is called,
> > do what you need to do with the element "name" and its value stored in
> > the class string object.  Then make sure you erase the class string
> > object before you return from the endElement() handler.
> >
> > Hope this helps.
> > Tuan
> >
> >
> > On Wed, 19 Dec 2001, Vinay Kakade wrote:
> >
> > > Hi,
> > >
> > > I have just started learning about Xerces C++ parser. I hope my question
> is appropriate for this mailing list.
> > >
> > > My question is: In the characters() method of HandlerBase class, can we
> get the name of the enclosing attribute in which in which this character
> data appear?
> > >
> > > For example:
> > >
> > > <tag1>
> > >     <tag2>someData</tag2>
> > > </tag1>
> > >
> > > In this xml file, when characters() method is called when "someData" is
> encountered, I want to retrieve name of enclosing attribute i.e. "tag2" in
> this case.
> > >
> > > Please let me know if there is any pre-implemented method to do this. I
> serached the documentation but could not find any.
> > >
> > > Thanks in Advance,
> > > -Vinay Kakade.
> > >
> > >
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: How to get attribute name in characters() method?

Posted by Vinay Kakade <vi...@veritas.com>.
Hi,

Thanks a lot for the reply.

In your reply, in the sample scenario, you have shown characters() method
called many times. But, I think the scenario is like this:

startElement( "tag1", ...)
characters("\n", 1)
startElement( "tag2", ...)
characters("\n", 1)
characters( "someData", 8 )
endElement( "tag2" )
characters("\n", 1)
endElement( "tag1" )
characters("\n", 1)

At least this is what the output of my program suggest. I am using SAX2
parser.

Please let me know:
1. How to avaoid the characters() method to be called for data that I want
to ignore, such as "\n" corresponding to a newline after start of a tag and
a newline after end of a tag?
2. For all the characters within "<tag2>" and "</tag2>", is the characters
method called only once, or the parser may call it more than once?

Thanks once again,
-Vinay Kakade.

----- Original Message -----
From: "Tuan Hoang" <tu...@hoang.homeip.net>
To: "Vinay Kakade" <vi...@veritas.com>
Cc: <xe...@xml.apache.org>
Sent: Thursday, December 20, 2001 10:32 AM
Subject: Re: How to get attribute name in characters() method?


> Using the SAX parser, you could get something similar to this:
>
> startElement( "tag1", ...)
> startElement( "tag2", ...)
> characters( "so", 2 )
> characters( "me", 2 )
> characters( "Dat", 3 )
> characters( "a", 1 )
> endElement( "tag2" )
> endElement( "tag1" )
>
> Basically in your handler for characters(), keep adding to a string
> object (I use a class data member).  When endElement() is called,
> do what you need to do with the element "name" and its value stored in
> the class string object.  Then make sure you erase the class string
> object before you return from the endElement() handler.
>
> Hope this helps.
> Tuan
>
>
> On Wed, 19 Dec 2001, Vinay Kakade wrote:
>
> > Hi,
> >
> > I have just started learning about Xerces C++ parser. I hope my question
is appropriate for this mailing list.
> >
> > My question is: In the characters() method of HandlerBase class, can we
get the name of the enclosing attribute in which in which this character
data appear?
> >
> > For example:
> >
> > <tag1>
> >     <tag2>someData</tag2>
> > </tag1>
> >
> > In this xml file, when characters() method is called when "someData" is
encountered, I want to retrieve name of enclosing attribute i.e. "tag2" in
this case.
> >
> > Please let me know if there is any pre-implemented method to do this. I
serached the documentation but could not find any.
> >
> > Thanks in Advance,
> > -Vinay Kakade.
> >
> >
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: How to get attribute name in characters() method?

Posted by Tuan Hoang <tu...@hoang.homeip.net>.
Using the SAX parser, you could get something similar to this:

startElement( "tag1", ...)
startElement( "tag2", ...)
characters( "so", 2 )
characters( "me", 2 )
characters( "Dat", 3 )
characters( "a", 1 )
endElement( "tag2" )
endElement( "tag1" )

Basically in your handler for characters(), keep adding to a string 
object (I use a class data member).  When endElement() is called, 
do what you need to do with the element "name" and its value stored in
the class string object.  Then make sure you erase the class string 
object before you return from the endElement() handler.

Hope this helps.
Tuan


On Wed, 19 Dec 2001, Vinay Kakade wrote:

> Hi,
> 
> I have just started learning about Xerces C++ parser. I hope my question is appropriate for this mailing list.
> 
> My question is: In the characters() method of HandlerBase class, can we get the name of the enclosing attribute in which in which this character data appear?
> 
> For example:
> 
> <tag1>
>     <tag2>someData</tag2>
> </tag1>
> 
> In this xml file, when characters() method is called when "someData" is encountered, I want to retrieve name of enclosing attribute i.e. "tag2" in this case.
> 
> Please let me know if there is any pre-implemented method to do this. I serached the documentation but could not find any.
> 
> Thanks in Advance,
> -Vinay Kakade.
>     
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org