Accessing character data (CDATA) of XML element : CDATA « XML « Java Tutorial






import java.util.Stack;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Main extends DefaultHandler {
  private Stack currentElement = new Stack();

  public void startElement(String uri, String localName, String qName, Attributes attrs)
      throws SAXException {
    currentElement.push(qName);
  }

  public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    currentElement.pop();
  }

  public void characters(char[] ch, int start, int length) throws SAXException {
    String cdata = new String(ch, start, length);
    System.out.println("Element '" + currentElement.peek() + "' contains text: " + cdata);
  }

  public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    SAXParser parser = factory.newSAXParser();
    parser.parse("sample.xml", new Main());
  }
}








33.23.CDATA
33.23.1.Accessing character data (CDATA) of XML element
33.23.2.Get character data (CDATA) from xml document
33.23.3.Converting CDATA Nodes into Text Nodes While Parsing an XML File
33.23.4.Adding a CDATA Section to a DOM Document
33.23.5.Set Data to Character Data
33.23.6.Get substring from CharacterData
33.23.7.Append and insert data to CharacterData
33.23.8.Delete data from CharacterData
33.23.9.Replace Data in CharacterData