Is there a way to serialize an object as an XML document?

John Zukowski

The Java serialization mechanism uses an efficient, compact format for encoding information about class structure and the values for instance variables. The eXtensible Markup Language (XML) provides a text-based approach for encoding structured data, and could also be used to encode the serialization information, albeit in a much less compact but more readable format. It should come as no surprise that this approach is being investigated to determine if it is appropriate under certain circumstances. The reference at the end of this answer provides a link to an article by Philip Milne and Kathy Walrath at The Swing Connection which illustrates such a technique by defining a persistence model which writes object graphs as XML documents. In this article, they define, among other things, an XML format for JavaBeans, and streams XMLOutputStream and XMLInputStream. The following is an example taken from that article which illustrates the XML format for a simple serialized JPanel object.

    <JAVA-OBJECT-ARCHIVE VERSION="0.1"> 
      <CLASS ID="JPanel" NAME="javax.swing.JPanel"/> 
      <CLASS ID="JButton" NAME="javax.swing.JButton"/> 
      <CLASS ID="Test" NAME="Test"/> 
      <CLASS ID="Rectangle" NAME="java.awt.Rectangle"/> 
      <CLASS ID="Integer" NAME="java.lang.Integer"/> 
      <CLASS ID="JTextField" NAME="javax.swing.JTextField"/> 
      <OBJECT ID="JPanel0" CLASS="JPanel"> 
        <OBJECT METHOD="add"> 
          <OBJECT ID="JButton0" CLASS="JButton"> 
            <OBJECT METHOD="addActionListener"> 
              <OBJECT CLASS="Test"/> 
            </OBJECT> 
            <OBJECT PROPERTY="bounds" CLASS="Rectangle"> 
              <OBJECT CLASS="Integer" VALUE="10"/> 
              <OBJECT CLASS="Integer" VALUE="20"/> 
              <OBJECT CLASS="Integer" VALUE="100"/> 
              <OBJECT CLASS="Integer" VALUE="20"/> 
            </OBJECT> 
            <OBJECT PROPERTY="text" VALUE="cut"/> 
          </OBJECT> 
        </OBJECT> 
        <OBJECT METHOD="add"> 
          <OBJECT ID="JTextField0" CLASS="JTextField"> 
            <OBJECT PROPERTY="nextFocusableComponent" 
               IDREF="JButton0"/> 
            <OBJECT PROPERTY="bounds" CLASS="Rectangle"> 
              <OBJECT CLASS="Integer" VALUE="30"/> 
              <OBJECT CLASS="Integer" VALUE="50"/> 
              <OBJECT CLASS="Integer" VALUE="200"/> 
              <OBJECT CLASS="Integer" VALUE="20"/> 
            </OBJECT> 
          </OBJECT> 
        </OBJECT> 
        <OBJECT PROPERTY="layout"/> 
        <OBJECT PROPERTY="bounds" CLASS="Rectangle"> 
          <OBJECT CLASS="Integer" VALUE="0"/> 
          <OBJECT CLASS="Integer" VALUE="0"/> 
          <OBJECT CLASS="Integer" VALUE="539"/> 
          <OBJECT CLASS="Integer" VALUE="366"/> 
        </OBJECT> 
      </OBJECT> 
    </JAVA-OBJECT-ARCHIVE> 

The use of XML in this manner is not officially part of the Java language, and a detailed discussion of this topic is beyond the scope of this FAQ. The article by Philip Milne and Kathy Walrath provides additional information for the interested reader [http://java.sun.com/products/jfc/tsc/articles/persistence/].

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact