简体   繁体   中英

Characters not displayed in endElement() of SAX parser

I have received the xml response from the server in the format below.(response edited)

1. Hello 2. World

Hello and World are present in separate lines within the message tag. I am able to display Hello World in characters(). But failed to get the same in endElement().

How do I get the content in multiple lines within the same tag in endElement() of SAX parser.

No, that is correct. characters() is where you are supposed to be getting Hello and World, not endElement() . End element signals the end of a tag and characters gets everything in between the tag.

If you want to do something in endElements() with 1.Hello 2.World then why not create a variable up top? For instance.

private String message = null;

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

      System.out.println(message); //or whatever you want to do with message

 }

@Override
public void characters(char ch[], int start, int length)
throws SAXException {
    if(isMessage){   //isMessage is set when you run startElement and it runs into the <message> block. 
        message = new String(ch, start, length);
        isMessage = false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM