to the main page about the tutorial  THE XML REVOLUTION  -  TECHNOLOGIES FOR THE FUTURE WEB back up next

A Business Card editor

A typical Java application is a domain-specific XML editor: We generalize the business card language to allow collections of business cards:

<cards>
  <card>
    <name>John Doe</name>
    <title>CEO, Widget Inc.</title>
    <email>john.doe@widget.com</email>
    <phone>(202) 456-1414</phone>
    <logo url="widget.gif" />
  </card>
  <card>
    <name>Michael Schwartzbach</name>
    <title>Associate Professor</title>
    <email>mis@brics.dk</email>
    <phone>+45 8610 8790</phone>
    <logo url="http://www.brics.dk/~mis/portrait.gif" />
  </card>
  <card>
    <name>Anders Møller</name>
    <title>Research Assistant Professor</title>
    <email>amoeller@brics.dk</email>
    <phone>+45 8942 3475</phone>
    <logo url="http://www.brics.dk/~amoeller/am.jpg"/>
  </card>
</cards>

We then write a Java program to edit such collections.

First, we need a high-level representation of a business card:

class Card {
  public String name, title, email, phone, logo;

  public Card(String name, String title, String email, String phone, String logo) {
    this.name = name;
    this.title = title;
    this.email = email;
    this.phone = phone;
    this.logo = logo;
  }
}

An XML document must then be translated into a vector of such objects:

Vector doc2vector(Document d) {
  Vector v = new Vector();
  Iterator i = d.getRootElement().getChildren().iterator();
  while (i.hasNext()) {
    Element e = (Element)i.next();
    String phone = e.getChildText("phone");
    if (phone==null) phone="";
    Element logo = e.getChild("logo");
    String url;
    if (logo==null) url = ""; else url = logo.getAttributeValue("url");
    Card c = new Card(e.getChildText("name"),  // exploit schema,
                      e.getChildText("title"), // assume validity
                      e.getChildText("email"),
                      phone,
                      url);
    v.add(c);
  }
  return v;
}

And back into an XML document:

Document vector2doc() {
  Element cards = new Element("cards");
  for (int i=0; i<cardvector.size(); i++) {
    Card c = (Card)cardvector.elementAt(i);
    if (c!=null) {
      Element card = new Element("card");
      Element name = new Element("name");
      name.addContent(c.name);
      card.addContent(name);
      Element title = new Element("title");
      title.addContent(c.title);
      card.addContent(title);
      Element email = new Element("email");
      email.addContent(c.email);
      card.addContent(email);
      if (!c.phone.equals("")) {
        Element phone = new Element("phone");
        phone.addContent(c.phone);
        card.addContent(phone);
      }
      if (!c.logo.equals("")) {
        Element logo = new Element("logo");
        logo.setAttribute("url",c.logo);
        card.addContent(logo);
      }
      cards.addContent(card);
    }
  }
  return new Document(cards);
}

A little logic and some GUI then completes the editor:

GUI

Compile with: javac -classpath xerces.jar:jdom.jar BCedit.java

This example contains some general observations:

back COPYRIGHT © 2000-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH next