RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Practical XML for Java Programs : Page 4

When dealing with XML, you need a convenient representation of the XML data in memory. This article offers Java programmers a solution to achieve this goal: an easy-to-use package for handling XML data in Java.

How the Package Works
The package for handling XML data in Java contains an interface (XmlData, see Listing 1), its basic implementation (BasicXmlData, see Listing 2), and XmlReader and XmlWriter classes. XmlReader (see Listing 3) uses SAX1 or SAX2, whichever is available, to parse input into BasicXmlData. It can read from InputStream, Reader, File, or from a String. XmlWriter (see Listing 4) neatly formats XmlData and sends it to a stream or a file.

The Interface
XmlData is the interface that defines basic XML data management functionality. It has four properties:

  1. Type — an immutable String
  2. Value — a String
  3. Attributes — a Map that can be set from AttributeList, or a String array, or from a Map
  4. A collection of XmlData kids

getAttribute(String name) retrieves individual attributes and setAttribute(String name, String value) sets them. Two methods, getId() and getName(), return the values of attributes "id" and "name".

getAllKids() and getKids(String type) return a Collection of kids. You can retrieve a kid with a specified attribute value: getKid(String type, String attributeName, String attributeValue); getKid(String type, String id) is the same as getKid(String type, "id", String attrValue). You can also addKid(XmlData), removeKid(XmlData), or removeKids(String type).

BasicXmlData is the basic implementation of XmlData. The following constructors are available (see Listing 2 for the full list):

  public BasicXmlData (String type, String value, Collection kids); 
  public BasicXmlData (String type, String value, String[] attrs, XmlData []kids); 
  public BasicXmlData (String type, String value, AttributeList attrs); 
  public BasicXmlData (String type, String value, Map attrs, Map byType); 
  public BasicXmlData (InputStream in); 
  public BasicXmlData (URL sourceUrl); 

The following are persistence methods:

  public void save(String filename); 
  public void save(File file); 
  public void save(OutputStream os); 

These three methods help to instantiate user-defined classes that implement XmlData. The user-defined classes should also have to contain a constructor that takes XmlData as a single argument. The simplest constructor is castKids(String type, Class clazz). Look at the following example:

public class Project extends BasicXmlData  { 
  private final static String POSTFIX = ".project.xml"; 
  private final        String filename; 
  public Project(String name) throws IOException, 
                                                             ClassNotFoundException { 
    super(new File(name + POSTFIX)); 
        filename = name + POSTFIX; 
    castKids("module", Class.forName("com.borland.catkit.Module")); 

As a result, all the kids of this project are of class Module.

Another method, cast(Map typemap, XmlData.Policy policy), can recursively cast a node with all its subnodes. The classes to cast to are defined in the map typemap, type —> Class. Policy specifies behavior on error, for example:

  • XmlData.Policy.SKIP_ON_ERROR specifies that nodes that fail to cast for some reason (missing constructors, class not found) are skipped.
  • XmlData.Policy.KEEP_ON_ERROR specifies that nodes that fail to cast are kept as is.
  • XmlData.Policy.THROW_ON_ERROR specifies that no action, exceptions are not intercepted.

Yet another casting method, cast(String packageName, BasicXmlData.Policy policy), casts the whole tree into classes within a specified package. Class names are the same as types of nodes. Look at the following example:

Project project = new BasicXmlData(new File("myproject.project.xml")).cast("untitled1",

The package may be incomplete for a full-time XML professional, and similar problems exist with Java (e.g., can't override an operator, can't manipulate memory allocation). So the choice for a practical Java programmer is either flood the code with complicated SAX callbacks and obscure DOM structures, or using this package, manipulate XML data for functions such as writing several lines to get Nasdaq quotes.

Vlad Patryshev is an R&D engineer at the Java Business Unit of Borland. He recently started the myjavatools.com project. by e-mail.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date