Zurück

XML Processing using XERCES Java Parser


Karl Seematter, Akadia AG, Information Technology, CH-3604 Thun
Phone: +41 33 335 86 24 / Fax: +41 33 335 86 25 /
EMail: karl.seematter@akadia.com


Using XML to transport information between different systems is nowadays a state of the art technology. Searching the WEB you'll find a huge set of examples how to validate and parse an XML document. But when it comes to the point where you have to create a XML document and send it as stream (i.g. character-stream, string, byte-array, etc.) to another system, it becomes difficult to find a proper example. Furthermore, different implementations of the DOM specification using different class names to do the same thing.

Here comes an example which creates a XML Document using DOM and transforms it to a String. We used the XERCES Java Parser V1_4_3 from apache.org.

Xerces (named after the Xerces Blue butterfly) provides world-class XML parsing and generation. Fully-validating parsers are available for both Java and C++, implementing the W3C XML and DOM (Level 1 and 2) standards, as well as the de facto SAX (version 2) standard. The parsers are highly modular and configurable. Initial support for XML Schema (draft W3C standard) is also provided.

Runing the example produces the following output

<?xml version="1.0" encoding="UTF-8"?>
<ProbeMsg>
  <TimeStamp>2001-11-30T09:08:07Z</TimeStamp>
  <ProbeId>1A6F</ProbeId>
  <ProbeValue ScaleUnit="mm">1245</ProbeValue>
</ProbeMsg>

Here is the Java Code: for ProbeMsg.java

import java.io.*;
import java.util.*;
import java.text.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
import org.apache.xerces.jaxp.DocumentBuilderImpl;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;

public class ProbeMsg {

   // XML tag's
   private static final String TAG_PROBE_MSG      = "ProbeMsg";
   private static final String TAG_TIMESTAMP      = "TimeStamp";
   private static final String TAG_PROPE_ID       = "ProbeId";
   private static final String TAG_PROPE_VALUE    = "ProbeValue";

   // XML Settings
   private static final String XML_VERSION        = "1.0";
   private static final String XML_ENCODING       = "UTF-8";

   // Format definitions
   private static final String DATE_TIME_FORMAT   =
     "yyyy-MM-dd'T'HH:mm:ss'Z'";

   // Variables
   private Date     msgTimeStamp  = null;
   private String   probeId       = "";
   private Integer  probeValue    = null;
   private Document xmlDoc        = null;
   private String   xmlStr        = null;

    // Constructor
    public ProbeMsg(Date   pTimeStamp
                  ,String pProbeId
                   ,int    pProbeValue ) {

      this.msgTimeStamp = pTimeStamp;
      this.probeId      = pProbeId;
      this.probeValue   = new Integer(pProbeValue);

     // Generate the XML Document using DOM
      this.generateXMLDocument();

      // Generate a XML String
      this.generateXMLString();
    }

    // Retrive probe message as XML string
   public String getXMLString() {
      return xmlStr;
    }

    // Generate a DOM XML document
    private void generateXMLDocument()
    {
     Element main;
     Element root;
     Element item;
      DateFormat timeStampFormat =
      new SimpleDateFormat( DATE_TIME_FORMAT );

    try {

      //Create a XML Document
      DocumentBuilderFactory dbFactory =
        DocumentBuilderFactoryImpl.newInstance();
      DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
      xmlDoc = docBuilder.newDocument();
    } catch(Exception e) {
        System.out.println("Error " + e);
    }

    // Create the root element
    root = xmlDoc.createElement(TAG_PROBE_MSG);

    // Add TimeStamp Element and its value
    item = xmlDoc.createElement(TAG_TIMESTAMP);
    item.appendChild(xmlDoc.createTextNode(
      timeStampFormat.format(msgTimeStamp)));
    root.appendChild(item);

    // Add ProbeId Element and its value
    item = xmlDoc.createElement(TAG_PROPE_ID);
    item.appendChild(xmlDoc.createTextNode(probeId));
    root.appendChild(item);

    // Add ProbeValue Element and its value
    item = xmlDoc.createElement(TAG_PROPE_VALUE);
    item.appendChild(xmlDoc.createTextNode(probeValue.toString() ));
    item.setAttribute("ScaleUnit", "mm");
    root.appendChild(item);

    // Add to the root Element
    xmlDoc.appendChild(root);

  }

  // Generate String out of the XML document object
  private void generateXMLString() {

    StringWriter  strWriter    = null;
    XMLSerializer probeMsgSerializer   = null;
    OutputFormat  outFormat    = null;

    try {
      probeMsgSerializer = new XMLSerializer();
      strWriter = new StringWriter();
      outFormat = new OutputFormat();

      // Setup format settings
      outFormat.setEncoding(XML_ENCODING);
      outFormat.setVersion(XML_VERSION);
      outFormat.setIndenting(true);
      outFormat.setIndent(4);

      // Define a Writer
      probeMsgSerializer.setOutputCharStream(strWriter);

      // Apply the format settings
      probeMsgSerializer.setOutputFormat(outFormat);

      // Serialize XML Document
      probeMsgSerializer.serialize(xmlDoc);
      this.xmlStr = strWriter.toString();
      strWriter.close();

    } catch (IOException ioEx) {
        System.out.println("Error " + ioEx);
    }
  }

  public static void main (String argv[]) {

    ProbeMsg pMsg = new ProbeMsg(new Date()   // Timestamp
                                ,"1A6F"       // Probe ID
                                ,1245);       // Probe Value
    System.out.println(pMsg.getXMLString() );
  }
}

Compile the Code with

javac -classpath ".;C:\java\xerces-1_4_3\xerces.jar" ProbeMsg.java

Run the Code with

java -classpath ".;C:\java\xerces-1_4_3\xerces.jar" ProbeMsg

Links and Download

Logging: log4j http://jakarta.apache.org/log4j/docs/index.html
Xml dom: jdom http://www.jdom.org/
Xml parser and others: xerces http://xml.apache.org/

Download above example