Jsp Test Sax Java

From Fxp Wiki

Jump to: navigation, search
 
package vendo;
 
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
 
public class TestSax extends DefaultHandler {
 
private Writer out;
 
 
private static final String PO_NAMESPACE_URI = "http://www.po.com";
  private StringBuffer elementContent = new StringBuffer();
  private String elementData;
  private boolean inElement = false;
  private int nestedElements = 0;
 
public void startElement (String uri, String localName,
          String qName, Attributes atts)
  throws SAXException {
  
    if ((localName.equals("name")) && (uri.equals(PO_NAMESPACE_URI))) {
      // Perform business-specific logic for po:name
      
    // Clear the current character content buffer
    elementContent.clear();
    inElement = true;
  } else {
      // Perform business-specific logic for all other elements
      
    // Ensure we don't pick up content for other elements
    if (inElement) {
        nestedElements++;
    }
  }
  }
  
  public void characters(char[] ch, int start, int len) throws SAXException {
    // Only get content if we're in the target element
  if (inElement && (nestedElements == 0)) {
    elementContent.append(new String(ch, start, len));
  }
  }
  
public void endElement (String uri, String localName, String qName)
    throws SAXException {
  
    if ((localName.equals("name")) && (uri.equals(PO_NAMESPACE_URI))) {
      // We're done
    elementData = elementContent.toString();
    inElement = false;
      
    // Do something with this data
  } else {
      // remove one from the nested element count, if appropriate
    if (inElement) {
        nestedElements--;
    }
  }
  }
  
  }
 
 
Personal tools