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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » XML » SAXScreenshots 
A Content Handler to Output a Sorted List as HTML

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 MyHtmlHandler implements ContentHandler {
  private boolean insideNameElement = false;

  private boolean insidePhoneElement = false;

  private boolean insideEmailElement = false;

  private Person person;

  private Vector personVec;

  private PrintWriter out;

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

  public void setDocumentLocator(Locator locator) {
  }

  public void startDocument() {
  }

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

    out.println("<html>");
    out.println("<head>");
    out.println("  <title>Persons</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<center><h1>Persons</h1><center>");
    out.println("<hr>");

    out.println("<center>");
    out.println("<table border cellspacing=0 cellpadding=5>");
    out.println("  <caption align=top>");
    out.println("    A List of Names with Phone and Email");
    out.println("  </caption>");
    out.println("  <tr>");
    out.println("    <th>Name</th>");
    out.println("    <th>Phone</th>");
    out.println("    <th>Email</th>");
    out.println("  </tr>");

    for (int i = 0; i < personVec.size(); i++) {
      Person p = (PersonpersonVec.elementAt(i);
      out.println("  <tr>");
      out.println("    <td>" + p.getName() "</td>");
      out.println("    <td>" + p.getPhone() "</td>");
      out.println("    <td>" + p.getEmail() "</td>");
      out.println("  </tr>");
    }
    out.println("</table>");
    out.println("</center>");
    out.println("</body>");
    out.println("</html>");
  }

  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) {
  }
}

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>Yin, 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
7. Simple lister - extract name and children tags
8. SAX Tree Validator
9. SAX Tree ViewerSAX Tree Viewer
ww_w_.___j__a___v__a__2___s_._c___o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.