java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Accessing character data (CDATA) of XML element E-mail
User Rating: / 22
PoorBest 

Besides elements defined in our DTD, XML has a set of already predefined ones. These are comments, character data (CDATA) etc. Character data is a text directly inserted between start and end tags of the element. It can mix with children tagged-elements.

A concrete implementation of SAX-parser defines whether it will collect all CDATA before passage to handler or it will pass it there by their occurence.

Example below specifies how to handle CDATA blocks:

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;

/**
 * This example demonstrates how to extract text (CDATA) information
 * from the element in the source XML-document.
 */

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

  // we push current element name:
  public void startElement (String uri, String localName,
    String qName, Attributes attrsthrows SAXException {

    currentElement.push(qName);
  }

  public void endElement(String namespaceURI, String localName,
    String qNamethrows SAXException {

    currentElement.pop();
  }

  // this method will be called for each character-section occurred;
  // if the element containes several CDATA-sections mixed with
  // child elements the number of calls depends on SAX-implementation:
  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) {
    try {
      // creates and returns new instance of SAX-implementation:
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setValidating(true);

      // create SAX-parser...
      SAXParser parser = factory.newSAXParser();


      parser.parse("sample.xml"new SampleOfCDataAccess());

    catch (Exception ex) {
      ex.printStackTrace(System.out);
    }
  }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.