A Content Handler to Output a Sorted List : SAX « XML « Java






A Content Handler to Output a Sorted List

      
import java.io.PrintWriter;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;

public class MyTextHandler implements ContentHandler {
  private boolean insideNameElement = false;

  private boolean insidePhoneElement = false;

  private boolean insideEmailElement = false;

  private Person person;

  private Vector personVec;

  private PrintWriter out;

  public MyTextHandler(PrintWriter out) {
    this.out = out;
    personVec = new Vector();
  }

  public void setDocumentLocator(Locator locator) {
  }

  public void startDocument() {
    putCols(" name", " phone", " email");
    putCols(" ----", " -----", " -----");
  }

  public void endDocument() {
    int k1 = 1;
    while (k1 < personVec.size()) {
      int k0 = k1 - 1;
      Person p0 = (Person) personVec.elementAt(k0);
      Person p1 = (Person) personVec.elementAt(k1);
      if (p0.getName().compareTo(p1.getName()) > 0) {
        personVec.setElementAt(p0, k1);
        personVec.setElementAt(p1, k0);
        if (k1 > 1)
          k1--;
      } else {
        k1++;
      }
    }

    for (int i = 0; i < personVec.size(); i++) {
      Person p = (Person) personVec.elementAt(i);
      putCols(p.getName(), p.getPhone(), p.getEmail());
    }
  }

  public void startPrefixMapping(String prefix, String uri) {
  }

  public void endPrefixMapping(String prefix) {
  }

  public void startElement(String namespaceURI, String localName,
      String qName, Attributes atts) {
    if (localName.equals("person")) {
      person = new Person();
    } else if (localName.equals("name")) {
      insideNameElement = true;
    } else if (localName.equals("phone")) {
      insidePhoneElement = true;
    } else if (localName.equals("email")) {
      insideEmailElement = true;
    }
  }

  public void endElement(String namespaceURI, String localName, String qName) {
    if (localName.equals("person")) {
      if (person != null)
        personVec.addElement(person);
    } else if (localName.equals("name")) {
      insideNameElement = false;
    } else if (localName.equals("phone")) {
      insidePhoneElement = false;
    } else if (localName.equals("email")) {
      insideEmailElement = false;
    }
  }

  public void characters(char[] ch, int start, int length) {
    String str = "";
    for (int i = start; i < start + length; i++)
      str += ch[i];
    if (insideNameElement)
      person.setName(str);
    else if (insidePhoneElement)
      person.setPhone(str);
    else if (insideEmailElement)
      person.setEmail(str);
  }

  public void ignorableWhitespace(char[] ch, int start, int length) {
  }

  public void processingInstruction(String target, String data) {
  }

  public void skippedEntity(String name) {
  }

  private void putCols(String col1, String col2, String col3) {
    String lout = col1;
    while (lout.length() < 25)
      lout += " ";
    lout += col2;
    while (lout.length() < 50)
      lout += " ";
    lout += col3;
    out.println(lout);
  }
}

// A Class for Holding Person Information

class Person {
  private String name = null;

  private String phone = null;

  private String email = null;

  public void setName(String value) {
    name = value;
  }

  public void setPhone(String value) {
    phone = value;
  }

  public void setEmail(String value) {
    email = value;
  }

  public String getName() {
    if (name == null)
      return ("none");
    return (name);
  }

  public String getPhone() {
    if (phone == null)
      return ("none");
    return (phone);
  }

  public String getEmail() {
    if (email == null)
      return ("none");
    return (email);
  }
}
//Example XML document
/*
 An XML Document Containing a Simple Contact List
Start example

<?xml version="1.0" standalone="yes"?>

<folks>
    <person>
        <phone>306 555-9999</phone>
        <email>joe@webserver.net</email>
        <name>Wang, Joe</name>
    </person>
    <person>
        <phone>704 555-0000</phone>
        <name>Pet, Rob</name>
        <email>rob@server.com</email>
    </person>
</folks>

*/


           
         
    
    
    
    
    
  








Related examples in the same category

1.Parsing XML Files with SAX
2.SAX Demo
3.Duplicates XML Files
4.A Program to Display the Input from a SAX ParserA Program to Display the Input from a SAX Parser
5.SAX Checker
6.A Content Handler to Output a Sorted List as HTML
7.Simple lister - extract name and children tags
8.SAX Tree Validator
9.SAX Tree ViewerSAX Tree Viewer
10.Accessing character data (CDATA) of XML element
11.Accessing features of the SAX parser implementation
12.Configuring SAX parser factory to produce alternate parser
13.Extracting attribute values from XML elements
14.Handling SAX errors during parsing
15.Using XML locator to indicate current parser position
16.Filter to write an XML document from a SAX event stream
17.XML utility methods that only depend on the JDK
18.Utility class for xml/sax handling
19.Create Xml Reader
20.Sax to DOM converter
21.Produce a SAX stream from a DOM Document
22.SAX2 writer: register a SAX2 ContentHandler and receive the callbacks in order to print a document that is parsed.
23.Register a SAX2 ContentHandler and receive callbacks to print information about the document.
24.Provides a complete trace of SAX2 events for files parsed.
25.The program prints all hyperlinks links of an XHTML web page