0

sample xml ,

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Issue>
     <Snippet>     
           sri;;
           hiil
           bye;
           tc;
    </Snippet>
    </Issue>

Is it possible to get the entire characters inside snippet tag ??

if this is the implementation,

    public void startElement(String uri, String localName,
             String qName, Attributes attributes) throws SAXException {
      temp = "";
      if (qName.equalsIgnoreCase("Issue")) {
             acct = new Account();

      public void endElement(String uri, String localName, String qName)
             throws SAXException {

      if (qName.equalsIgnoreCase("Issue")) {
             // add it to the list
             accList.add(acct);
       else if(qName.equalsIgnoreCase("Snippet"))
           {
               acct.setPrimarySnippet(temp);
           }

O/p is tc; but i need entire values inside the snippet tag to get printed.

arraylist is used . Getter and setter methods used to stre and retrieve values.

1
  • how are you getting the value "temp" that you are setting in the endElement() method? Commented Apr 25, 2013 at 15:14

3 Answers 3

2

use the method "characters".

http://docs.oracle.com/javase/1.5.0/docs/api/org/xml/sax/helpers/DefaultHandler.html#characters(char[], int, int)

Meaning, you have to implement the startElement and endElement methods (to signal that you're entering and exiting the 'Snippet' tag, and then the characters method will return the characters.

  public void startElement(String uri, String localName,
             String qName, Attributes attributes) throws SAXException {
      temp = "";
      if (qName.equalsIgnoreCase("Issue")) {
             someFlagVariable = true;

 public void endElement(String uri, String localName, String qName)
             throws SAXException {
      if (qName.equalsIgnoreCase("Issue")) {
             someFlagVariable = false;
      }
 }
public void characters(char[] ch,
                   int start,
                   int length)
            throws SAXException{
   if (someFlagVariable ){
       String content = new String(ch, start, length).trim(); //this is your content
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You should write the characters of ch into a StringBuilder and create the string on endElement. This ensures to get the full string and not only a chunk of it.
0

Yes.

You should be grabbing the value for "temp" (the value you set as the primary snippet) in the characters() method.

However, you should be aware that there isn't a guarantee as to when characters() will be executed, and may be called several times within a single node. So within you override of the characters() method you need to build a string up - that way when you get to endElement() you will have the complete value.

You can see an example implementation here

But you basically want something like:

StringBuffer chars = new StringBuffer();

public void startElement(String uri, String localName, String qName, Attributes atts) {
    chars = new StringBuffer();
}

public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.equalsIgnoreCase("Issue")) {
        // add it to the list
        accList.add(acct);
     else if(qName.equalsIgnoreCase("Snippet")){
        acct.setPrimarySnippet(chars);
     }
}

public void characters(char ch[], int start, int length) {
    chars.append(new String(ch, start, length));
}

(Although note, the above only works if you only care about text in leaf nodes - as we are new'ing the stringBuffer on startElement(), if you want the text of non-leaf nodes then you would need to introduce flags in the startElement() method so you only re-instantiate the stringbuffer at the right time)

Comments

0

1) to print text inside Snippet you should implement

public void characters(char ch[], int start, int length)

2) text inside Snippet contains several lines, with SAX you will be getting each line separately, this behaviour is documented in SAX API, it may depend on provider, but at least with JDK default SAX parser you cannot change it. Try StAX, it has XMLInputFactory.IS_COALESCING option that fixes this problem.

4 Comments

Yes i have implemented characters method and sorry i forgot to include here. Yet only last line in the snippet tag is getting printed. Is this default behavior of SAX parser? 'll StAX meet my requirement ??
It's easier to do it with StAX. But SAX will work too, you need to collect the chuncks SAX sends to characters() in a StringBuilder like rhinds suggests.
Fine . is it possible to use StAX in addition to my implementation or i need to approach the entire thing using StAX apart from SAX?
It works with SAX as rhinds suggests. Thanks for your suggestion too.. I'll try to implement with StaX too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.