http://www.javazoom.net/services/newsletter/xmlgeneration.html

<html>
<head>
  <META NAME="title" CONTENT="XML generation">
  <META NAME="description" CONTENT="Find out how to generate XML through XML parser/processor. Make your java code standard, portable and memory friendly unsing JAXP and SAX. Make you code JDK1.4 compliant.">
  <META NAME="keywords" CONTENT="XML,Java,xerces,J2EE,Application server,xalan,xerces2,jdk1.4,parser,processor,xalan2,crimson,jdom,Tomcat,Servlets,JSP,jaxp,jaxp1.1,document,factory,dom,sax,handler,Websphere,api,Free,Web,Services,Tools">
  <TITLE>Tutorial : XML generation with Java</TITLE>
<STYLE TYPE="text/css">
<!--
a {  font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF3300; text-decoration: none; font-weight: normal}
a:hover {  font-family: Verdana, Arial, Helvetica, sans-serif; color: #333333; text-decoration: underline}
.style1 {color: #0000CC}
-->
</STYLE>
<script language="JavaScript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>

<body bgcolor="#FFFFFF" onLoad="MM_preloadImages('../../jsmenu/applets1.gif','../../jsmenu/servlets1.gif','../../jsmenu/projects1.gif','../../jsmenu/services1.gif','../../jsmenu/links1.gif','../../jsmenu/about1.gif')" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
<script language="JavaScript" src="img/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
<table width="770" border="0" name="Main" cellpadding="0" cellspacing="0" vspace="0" hspace="0">
  <tr>
    <td>
      <table width="770" border="0" name="Head" height="86" cellpadding="0" cellspacing="0" vspace="0" hspace="0">
        <tr>
          <td width="279" height="86"><a href="http://www.javazoom.net" target="_top"><img src="../../finemenu/title.gif" width="279" height="86" border="0" alt="JavaZOOM Web Site" vspace="0" hspace="0"></a></td>
          <td width="491" height="86" align="center"><font size="-2">
<script type="text/javascript"><!--
google_ad_client = "pub-6941759278582827";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_color_border = "B0E0E6";
google_color_bg = "FFFFFF";
google_color_link = "000000";
google_color_url = "336699";
google_color_text = "333333";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></font></td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td width="150" height="514">
      <table width="770" border="0" height="514" name="Body" cellpadding="0" cellspacing="0" vspace="0" hspace="0">
        <tr nowrap>
          <td height="514" width="146" valign="top">
            <table width="146" border="0" height="514" cellpadding="0" cellspacing="0" vspace="0" hspace="0">
              <tr nowrap align="center">
                <td width="146" height="146" background="../../finemenu/middle.gif">
                  <a href="../../applets/applets.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('applets','','../../jsmenu/applets1.gif',1)"><img name="applets" border="0" src="../../jsmenu/applets0.gif" width="110" height="20" alt="Applets !"></a><a href="../../jzservlets/servlets.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Servlets','','../../jsmenu/servlets1.gif',1)"><img name="Servlets" border="0" src="../../jsmenu/servlets0.gif" width="100" height="20"></a><br>
                  <a href="../../projects.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('projects','','../../jsmenu/projects1.gif',1)"><img name="projects" border="0" src="../../jsmenu/projects0.gif" width="100" height="20" alt="Open projects"></a><br>
                  <a href="../services.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('services','','../../jsmenu/services1.gif',1)"><img name="services" border="0" src="../../jsmenu/services0.gif" width="110" height="20" alt="Services"></a><br>
                  <a href="../../links/links.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('links','','../../jsmenu/links1.gif',1)"><img name="links" border="0" src="../../jsmenu/links0.gif" width="100" height="20" alt="Links"></a><br>
                  <a href="../../links/links.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('about','','../../jsmenu/about1.gif',1)"><img name="about" border="0" src="../../jsmenu/about0.gif" width="100" height="20" alt="About JavaZOOM"></a></td>
              </tr>
              <tr nowrap align="center">
                <td width="146" height="167" background="../../finemenu/bottom.gif">
                  <p><a href="../../applets/jads21/jads.html" target="_self"><font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><b>Multiple Ads System</b></font><br>
                  <img src="../../pub/ads.gif" width="104" height="56" border="0" alt="J-Ads 2.1 Applet" name="Ads"></a></p>
                </td>
              </tr>
              <tr nowrap align="center" valign="top">
                <td width="146" height="201">
                  <form method="post" action="http://www.yourmailinglistprovider.com/subscribe.php?javazoom">
                    <table border="0">
                      <tr valign="middle" align="center" nowrap>
                        <td colspan="2"><font color="#000066" size="-1" face="Verdana, Arial, Helvetica, sans-serif"><b>Email newsletter</b></font><font size="-1">&nbsp;</font></td>
                      </tr>
                      <tr>
                        <td align="center">
                          <input type="text" name="YMP[0]" size="11" maxlength="50">
                        </td>
                        <td align="center">
                          <input type="submit" value="Ok" name="Submit">
                        </td>
                      </tr>
                      <tr align="left">
                        <td colspan="2"><font size="-2" face="Arial, Helvetica, sans-serif">Powered by<br>
YourMailinglistProvider.com</font></td>
                      </tr>
                    </table>
                  </form>
                  <p>
                  <p>
                  <br>
                  <a href="http://www.wantjava.com" target="_blank"><img src="../../pub/wjlogo13.gif" width="93" height="33" border="1" alt="Recommended Host"></a></td>
              </tr>
            </table>
          </td>
          <td width="624" align="center" valign="top">
            <p>
            <p>
            <center>
              <table width="620" border="0" cellspacing="0" cellpadding="0">
                <tr bgcolor="#000033" align="center" background="../../images/bg.gif">
                  <td height="20" background="../../images/bg.gif"><font color="#FFFFFF" size="-1" face="Verdana, Arial, Helvetica, sans-serif"><b>Tutorial - 07/05/2002</b></font></td>
                </tr>
                <tr bgcolor="#CCCCCC">
                  <td>
                    <table width="100%" border="0" cellspacing="1" cellpadding="0">
                      <tr bgcolor="#FFFFFF">
                        <td>
                          <table width="100%" border="0" cellspacing="5" cellpadding="0">
                            <tr>
                              <td>
                                <p><b><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">Tutorial
                                  : XML generation with JAVA</font></b><br>
                                  <br>
                                  <font face="Verdana, Arial, Helvetica, sans-serif" size="-1">XML
                                  developers used to rely on XML parsers to read
                                  XML files. They also used to rely on XML processors
                                  to transform XML to *ML (HTML, XML ...). However,
                                  most of them forget these tools to generate
                                  XML from scratch. They should not ...</font></p>
                                <p><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">Below,
                                  the XML file (<a href="xmlgeneration/users.xml">users.xml</a>)
                                  you want to generate from input data. It's just
                                  a list of user. Each user has an ID a TYPE and
                                  a NAME (Full definition is available in <a href="xmlgeneration/users.dtd">users.dtd</a>).</font></p>
                                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td bgcolor="#333333">
                                      <table width="100%" border="0" cellspacing="1" cellpadding="1">
                                        <tr bgcolor="#CCCCCC">
                                          <td bgcolor="#CCCCCC" align="center">
                                            <div align="center"><font color="#000066" size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b>Input data</b></font></div>
                                          </td>
                                          <td align="center">
                                            <div align="center"><font color="#000066" size="-2" face="Verdana, Arial, Helvetica, sans-serif"><b>Matching valid XML file : users.xml</b></font></div>
                                          </td>
                                        </tr>
                                        <tr>
                                          <td bgcolor="#FFFFFF" align="center" valign="top">
                                            <table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolor="#CCCCCC">
                                              <tr align="center" bgcolor="#CCCCCC">
                                                <td bgcolor="#EEEEEE"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">NAME</font></td>
                                                <td bgcolor="#EEEEEE"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">ID</font></td>
                                                <td bgcolor="#EEEEEE"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">TYPE</font></td>
                                              </tr>
                                              <tr align="center">
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">Tim@Home</font></td>
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">PWS122</font></td>
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">customer</font></td>
                                              </tr>
                                              <tr align="center">
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">Jack&amp;Moud</font></td>
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">MX787</font></td>
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">manager</font></td>
                                              </tr>
                                              <tr align="center">
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">John
                                                  D'o&eacute;</font></td>
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">A4Q45</font></td>
                                                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">employee</font></td>
                                              </tr>
                                            </table>

                                          </td>
                                          <td bgcolor="#FFFFFF"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">&lt;?xml
                                            version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<br>
                                            &lt;!DOCTYPE USERS SYSTEM &quot;users.dtd&quot;&gt;<br>
                                            &lt;USERS&gt;<br>
                                            &nbsp;&nbsp;&lt;USER ID=&quot;PWD122&quot;
                                            TYPE=&quot;customer&quot;&gt;Tim@Home&lt;/USER&gt;<br>
                                            &nbsp;&nbsp;&lt;USER ID=&quot;MX787&quot;
                                            TYPE=&quot;manager&quot;&gt;Jack&amp;amp;Moud&lt;/USER&gt;<br>
                                            &nbsp;&nbsp;&lt;USER ID=&quot;A4Q45&quot;
                                            TYPE=&quot;employee&quot;&gt;John
                                            D&amp;apos;o&eacute;&lt;/USER&gt;<br>
                                            &lt;/USERS&gt;</font></td>
                                        </tr>
                                      </table>
                                    </td>
                                  </tr>
                                </table>
                                <p><font face="Verdana, Arial, Helvetica, sans-serif"><b>A</b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>n</b>
                                  XML novice developer could write the following
                                  code to quickly generate users.xml :<br>
                                  </font></p>
                                <table width="100%" border="0" cellspacing="1" cellpadding="1">
                                  <tr>
                                    <td bgcolor="#66CCFF" align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>(1)
                                      - Serialization to file output stream -
                                      </b></font></td>
                                  </tr>
                                  <tr>
                                    <td bgcolor="#EEEEFF"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="#0000FF">[...]</font><font face="Verdana, Arial, Helvetica, sans-serif" size="-2"><br>
                                      String ENCODING = &quot;<font color="#CC0000">ISO-8859-1</font>&quot;;<br>
                                      String[] id = {&quot;<font color="#CC0000">PWD122</font>&quot;,&quot;<font color="#CC0000">MX787</font>&quot;,&quot;<font color="#CC0000">A4Q45</font>&quot;};<br>
                                      String[] type = {&quot;<font color="#CC0000">customer</font>&quot;,&quot;<font color="#CC0000">manager</font>&quot;,&quot;<font color="#CC0000">employee</font>&quot;};<br>
                                      String[] desc = {&quot;<font color="#CC0000">Tim@Home</font>&quot;,&quot;<font color="#CC0000">Jack&amp;Moud</font>&quot;,&quot;<font color="#CC0000">John
                                      D'o&eacute;</font>&quot;};<br>
                                      PrintWriter out = new PrintWriter(new FileOutputStream(&quot;users.xml&quot;));<br>
                                      out.println(&quot;&lt;?xml version=\&quot;1.0\&quot;
                                      encoding=\&quot;&quot;+ENCODING+&quot;\&quot;?&gt;&quot;);<br>
                                      out.println(&quot;&lt;!DOCTYPE USERS SYSTEM
                                      \&quot;<font color="#CC0000">users.dtd</font>\&quot;&gt;&quot;);<br>
                                      out.println(&quot;&lt;USERS&gt;&quot;);<br>
                                      for (int i=0;i&lt;id.length;i++)<br>
                                      {<br>
                                      &nbsp;&nbsp;out.println(&quot;&lt;USER ID=\&quot;&quot;+id[i]+&quot;\&quot;
                                      TYPE=\&quot;&quot;+type[i]+&quot;\&quot;&gt;&quot;+desc[i]+&quot;&lt;/USER&gt;&quot;);<br>
                                      }<br>
                                      out.println(&quot;&lt;/USERS&gt;&quot;);<br>
                                      <font color="#0000FF">[...]</font></font></td>
                                  </tr>
                                </table>
                                <p><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">
                                  Unfortunately, this code does <u>not generate
                                  valid XML</u>. Look at &quot;Jack&amp;Moud&quot;
                                  username, it has to be translated to &quot;Jack&amp;amp;Moud&quot;
                                  because of '&amp;' special character (called
                                  predefined entity). In addition to these specials
                                  characters, the developer has to take into account
                                  XML processing instructions (i.e. : &lt;?xml-stylesheet
                                  type=&quot;text/xsl&quot; href=&quot;users.xsl&quot;?&gt;),
                                  entity references, comments and cd-data section
                                  (An XML reference syntax is available in PDF
                                  format <a href="http://www.mulberrytech.com/quickref/XMLquickref.pdf">here</a>).</font><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><br>
                                  <br>
                                  </font><font face="Verdana, Arial, Helvetica, sans-serif"><b>O</b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>b</b>viously,
                                  Java XML libraries (parsers and/or processors)
                                  should be used to generate well-formed and valid
                                  XML. The first solution creates a DOM document
                                  from scratch and serialize it to XML. The following
                                  code uses Xerces to do so :<br>
                                  </font></p>
                                <table width="100%" border="0" cellspacing="1" cellpadding="1">
                                  <tr>
                                    <td bgcolor="#66CCFF" align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>(2)
                                      - DOM + Xerces serialization to file output
                                      stream - </b></font></td>
                                  </tr>
                                  <tr>
                                    <td bgcolor="#EEEEFF"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">import
                                      java.io.*;<br>
                                      <font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="#006633">//
                                      DOM</font><br>
import org.w3c.dom.*; <font color="#006633"><br>
                                      // Xerces classes.</font><br>
                                      import org.apache.xerces.dom.DocumentImpl;<br>
                                      import org.apache.xml.serialize.*;<br>
                                      <font color="#0000FF">[...]</font><br>
                                      Element e = null;<br>
                                      Node n = null;<br>
                                      <font color="#006633">// Document (Xerces
                                      implementation only).</font><br>
                                      Document xmldoc= new DocumentImpl();<br>
                                      <font color="#006633">// Root element.</font><br>
                                      Element root = xmldoc.createElement(&quot;USERS&quot;);<br>
                                      String[] id = {&quot;<font color="#CC0000">PWD122</font>&quot;,&quot;<font color="#CC0000">MX787</font>&quot;,&quot;<font color="#CC0000">A4Q45</font>&quot;};<br>
                                      String[] type = {&quot;<font color="#CC0000">customer</font>&quot;,&quot;<font color="#CC0000">manager</font>&quot;,&quot;<font color="#CC0000">employee</font>&quot;};<br>
                                      String[] desc = {&quot;<font color="#CC0000">Tim@Home</font>&quot;,&quot;<font color="#CC0000">Jack&amp;Moud</font>&quot;,&quot;<font color="#CC0000">John
                                      D'o&eacute;</font>&quot;};<br>
                                      for (int i=0;i&lt;id.length;i++)<br>
                                      {<br>
                                      <font color="#006633">&nbsp;&nbsp;// Child
                                      i.</font><br>
                                      &nbsp;&nbsp;e = xmldoc.createElementNS(null,
                                      &quot;USER&quot;);<br>
                                      &nbsp;&nbsp;e.setAttributeNS(null, &quot;ID&quot;,
                                      id[i]);<br>
                                      &nbsp;&nbsp;e.setAttributeNS(null, &quot;TYPE&quot;,
                                      type[i]);<br>
                                      &nbsp;&nbsp;n = xmldoc.createTextNode(desc[i]);<br>
                                      &nbsp;&nbsp;e.appendChild(n);<br>
                                      &nbsp;&nbsp;root.appendChild(e);<br>
                                      }<br>
                                      xmldoc.appendChild(root);<br>
                                      FileOutputStream fos = new FileOutputStream(filename);<br>
                                      <font color="#006633">// XERCES 1 or 2 additionnal
                                      classes.</font><br>
                                      OutputFormat of = new OutputFormat(&quot;XML&quot;,&quot;<font color="#CC0000">ISO-8859-1</font>&quot;,true);<br>
                                      of.setIndent(1);<br>
                                      of.setIndenting(true);<br>
                                      of.setDoctype(null,&quot;<font color="#CC0000">users.dtd</font>&quot;);<br>
                                      XMLSerializer serializer = new XMLSerializer(fos,of);<br>
                                      <font color="#006633">// As a DOM Serializer</font><br>
                                      serializer.asDOMSerializer();<br>
                                      serializer.serialize( xmldoc.getDocumentElement()
                                      );<br>
                                      fos.close();<br>
                                      <font color="#0000FF">[...]</font></font></td>
                                  </tr>
                                </table>
                                <p><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">This
                                  solution works nice for small XML files but
                                  it should be avoided for big files because it's
                                  memory consuming. Indeed, the full DOM document
                                  (Element, Node, Attributes ...) is in memory
                                  before being serialized. It's the major drawback
                                  of DOM.<br>
                                  <br>
                                  </font><font face="Verdana, Arial, Helvetica, sans-serif"><b>A</b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">
                                  <b>m</b>emory-friendly solution is to serialize
                                  the XML file on the fly through SAX. <br>
                                  The code below uses Xerces to do so :</font></p>
                                <table width="100%" border="0" cellspacing="1" cellpadding="1">
                                  <tr>
                                    <td bgcolor="#66CCFF" align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>(3)
                                      - SAX + Xerces serialization to file output
                                      stream - </b></font></td>
                                  </tr>
                                  <tr>
                                    <td bgcolor="#EEEEFF"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">import
                                      java.io.*;<br>
                                      <font color="#006633">// Xerces 1 or 2 additional
                                      classes.</font><br>
                                      import org.apache.xml.serialize.*;<br>
                                      import org.xml.sax.*;<br>
                                      import org.xml.sax.helpers.*;<br>
                                      <font color="#0000FF">[...]</font><br>
                                      FileOutputStream fos = new FileOutputStream(filename);<br>
                                      <font color="#006633">// XERCES 1 or 2 additionnal
                                      classes.</font><br>
                                      OutputFormat of = new OutputFormat(&quot;XML&quot;,&quot;<font color="#CC0000">ISO-8859-1</font>&quot;,true);<br>
                                      of.setIndent(1);<br>
                                      of.setIndenting(true);<br>
                                      of.setDoctype(null,&quot;<font color="#CC0000">users.dtd</font>&quot;);<br>
                                      XMLSerializer serializer = new XMLSerializer(fos,of);<br>
                                      <font color="#006633">// SAX2.0 ContentHandler.</font><br>
                                      ContentHandler hd = serializer.asContentHandler();<br>
                                      hd.startDocument();<br>
                                      <font color="#006633">// Processing instruction
                                      sample.<br>
                                      //hd.processingInstruction(&quot;xml-stylesheet&quot;,&quot;type=\&quot;text/xsl\&quot;
                                      href=\&quot;users.xsl\&quot;&quot;);</font><br>
                                      <font color="#006633">// USER attributes</font>.<br>
                                      AttributesImpl atts = new AttributesImpl();<br>
                                      <font color="#006633">// USERS tag.</font><br>
                                      hd.startElement(&quot;&quot;,&quot;&quot;,&quot;USERS&quot;,atts);<br>
                                      <font color="#006633">// USER tags.</font><br>
                                      String[] id = {&quot;<font color="#CC0000">PWD122</font>&quot;,&quot;<font color="#CC0000">MX787</font>&quot;,&quot;<font color="#CC0000">A4Q45</font>&quot;};<br>
                                      String[] type = {&quot;<font color="#CC0000">customer</font>&quot;,&quot;<font color="#CC0000">manager</font>&quot;,&quot;<font color="#CC0000">employee</font>&quot;};<br>
                                      String[] desc = {&quot;<font color="#CC0000">Tim@Home</font>&quot;,&quot;<font color="#CC0000">Jack&amp;Moud</font>&quot;,&quot;<font color="#CC0000">John
                                      D'o&eacute;</font>&quot;};<br>
                                      for (int i=0;i&lt;id.length;i++)<br>
                                      {<br>
                                      &nbsp;&nbsp;atts.clear();<br>
                                      &nbsp;&nbsp;atts.addAttribute(&quot;&quot;,&quot;&quot;,&quot;ID&quot;,&quot;CDATA&quot;,id[i]);<br>
                                      &nbsp;&nbsp;atts.addAttribute(&quot;&quot;,&quot;&quot;,&quot;TYPE&quot;,&quot;CDATA&quot;,type[i]);<br>
                                      &nbsp;&nbsp;hd.startElement(&quot;&quot;,&quot;&quot;,&quot;USER&quot;,atts);<br>
                                      &nbsp;&nbsp;hd.characters(desc[i].toCharArray(),0,desc[i].length());<br>
                                      &nbsp;&nbsp;hd.endElement(&quot;&quot;,&quot;&quot;,&quot;USER&quot;);<br>
                                      }<br>
                                      hd.endElement(&quot;&quot;,&quot;&quot;,&quot;USERS&quot;);<br>
                                      hd.endDocument();<br>
                                      fos.close();<br>
                                      <font color="#0000FF">[...]</font></font></td>
                                  </tr>
                                </table>
                                <p><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">SAX
                                  is an event-based API. Basically, to read an
                                  XML file, you implement ContentHandler interface.
                                  startElement(), characters(...), endElement()
                                  methods are called when XML file is parsed.
                                  Xerces provides a reverse solution. Developer
                                  calls startElement(...), characters(...), endElement(...)
                                  SAX methods to generate the XML file.<br>
                                  <br>
                                  </font><font face="Verdana, Arial, Helvetica, sans-serif"><b>S</b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><b>a</b>mples
                                  above need Xerces library but what about others
                                  libraries : Crimson, JDOM, <br>
                                  Xalan2, JDK 1.4 .... ? How to select an XML
                                  parser and/or an XML processor ? How to make
                                  a java code not dependant from a specific library
                                  ?<br>
                                  A good solution is JAXP 1.1. <b>JAXP 1.1</b>
                                  is an API developped by SUN. <b>It allows to
                                  plug any XML parser and/or processor</b> and
                                  &quot;write once, run anywhere&quot; your Java/XML
                                  code. Implementation of this API is available
                                  in JDK1.4 and provided by Xalan2 too.<br>
                                  Here are similar samples for XML generation
                                  with JAXP 1.1 :<br>
                                  </font></p>
                                <table width="100%" border="0" cellspacing="1" cellpadding="1">
                                  <tr>
                                    <td bgcolor="#66CCFF" align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">
                                      <b>(4) - JAXP + DOM + Serialization to servlet
                                      output stream : JDK 1.4 compliant - </b></font></td>
                                  </tr>
                                  <tr>
                                    <td bgcolor="#EEEEFF"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">import
                                      java.io.*;<br>
                                      <font color="#006633">// DOM classes.</font><br>
                                      import org.w3c.dom.*;<br>
                                      <font color="#006633">//JAXP 1.1</font><br>
                                      import javax.xml.parsers.*;<br>
                                      import javax.xml.transform.*;<br>
                                      import javax.xml.transform.stream.*;<br>
                                      import javax.xml.transform.dom.*; <br>
                                      <font color="#0000FF">[...]</font><br>
                                      <font color="#006633">// PrintWriter from
                                      a Servlet</font><br>
                                      PrintWriter out = response.getWriter();<font color="#006633"><br>
                                      // Create XML DOM document (Memory consuming).</font><br>
                                      Document xmldoc = null;<br>
                                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();<br>
                                      DocumentBuilder builder = factory.newDocumentBuilder();<br>
                                      DOMImplementation impl = builder.getDOMImplementation();<br>
                                      Element e = null;<br>
                                      Node n = null;<br>
                                      <font color="#006633">// Document.</font><br>
                                      xmldoc = impl.createDocument(null, &quot;USERS&quot;,
                                      null);<br>
                                      <font color="#006600">// Root element.</font><br>
                                      Element root = xmldoc.getDocumentElement();<br>
                                      String[] id = {&quot;<font color="#CC0000">PWD122</font>&quot;,&quot;<font color="#CC0000">MX787</font>&quot;,&quot;<font color="#CC0000">A4Q45</font>&quot;};<br>
                                      String[] type = {&quot;<font color="#CC0000">customer</font>&quot;,&quot;<font color="#CC0000">manager</font>&quot;,&quot;<font color="#CC0000">employee</font>&quot;};<br>
                                      String[] desc = {&quot;<font color="#CC0000">Tim@Home</font>&quot;,&quot;<font color="#CC0000">Jack&amp;Moud</font>&quot;,&quot;<font color="#CC0000">John
                                      D'o&eacute;</font>&quot;};<br>
                                      for (int i=0;i&lt;id.length;i++)<br>
                                      {<br>
                                      <font color="#006633">&nbsp;&nbsp;// Child
                                      i.</font><br>
                                      &nbsp;&nbsp;e = xmldoc.createElementNS(null,
                                      &quot;USER&quot;);<br>
                                      &nbsp;&nbsp;e.setAttributeNS(null, &quot;ID&quot;,
                                      id[i]);<br>
                                      &nbsp;&nbsp;e.setAttributeNS(null, &quot;TYPE&quot;,
                                      type[i]);<br>
                                      &nbsp;&nbsp;n = xmldoc.createTextNode(desc[i]);<br>
                                      &nbsp;&nbsp;e.appendChild(n);<br>
                                      &nbsp;&nbsp;root.appendChild(e);<br>
                                      }<br>
                                      <font color="#006633">// Serialisation through
                                      Tranform.</font><br>
                                      DOMSource domSource = new DOMSource(xmldoc);<br>
                                      StreamResult streamResult = new StreamResult(out);<br>
                                      TransformerFactory tf = TransformerFactory.newInstance();<br>
                                      Transformer serializer = tf.newTransformer();<br>
                                      serializer.setOutputProperty(OutputKeys.ENCODING,<font color="#CC0000">&quot;ISO-8859-1</font>&quot;);<br>
                                      serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,&quot;<font color="#CC0000">users.dtd</font>&quot;);<br>
                                      serializer.setOutputProperty(OutputKeys.INDENT,&quot;yes&quot;);<br>
                                      serializer.transform(domSource, streamResult);
                                      <br>
                                      <font color="#0000FF">[...]</font></font></td>
                                  </tr>
                                </table>
                                <br>
                                <table width="100%" border="0" cellspacing="1" cellpadding="1">
                                  <tr>
                                    <td bgcolor="#66CCFF" align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">
                                      <b>(5) - JAXP + SAX + Serialization to servlet
                                      output stream : JDK 1.4 compliant - </b></font></td>
                                  </tr>
                                  <tr>
                                    <td bgcolor="#EEEEFF">
                                      <p><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">import
                                        java.io.*;<br>
                                        <font color="#006633">// SAX classes.</font><br>
                                        import org.xml.sax.*;<br>
                                        import org.xml.sax.helpers.*;<br>
                                        <font color="#006633">//JAXP 1.1</font><br>
                                        import javax.xml.parsers.*;<br>
                                        import javax.xml.transform.*;<br>
                                        import javax.xml.transform.stream.*;<br>
                                        import javax.xml.transform.sax.*; <br>
                                        <font color="#0000FF">[...]</font><br>
                                        <font color="#006633">// PrintWriter from
                                        a Servlet</font><br>
                                        PrintWriter out = response.getWriter();<br>
                                        StreamResult streamResult = new StreamResult(out);<br>
                                        SAXTransformerFactory tf = (SAXTransformerFactory)
                                        SAXTransformerFactory.newInstance();<br>
                                        <font color="#006633">// SAX2.0 ContentHandler.</font><br>
                                        TransformerHandler hd = tf.newTransformerHandler();<br>
                                        Transformer serializer = hd.getTransformer();<br>
                                        serializer.setOutputProperty(OutputKeys.ENCODING,&quot;<font color="#CC0000">ISO-8859-1</font>&quot;);<br>
                                        serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,&quot;<font color="#CC0000">users.dtd</font>&quot;);<br>
                                        serializer.setOutputProperty(OutputKeys.INDENT,&quot;yes&quot;);<br>
                                        hd.setResult(streamResult);<br>
                                        hd.startDocument();<br>
                                        AttributesImpl atts = new AttributesImpl();<br>
                                        <font color="#006633">// USERS tag.</font><br>
                                        hd.startElement(&quot;&quot;,&quot;&quot;,&quot;USERS&quot;,atts);<br>
                                        <font color="#006633">// USER tags.</font><br>
                                        String[] id = {&quot;<font color="#CC0000">PWD122</font>&quot;,&quot;<font color="#CC0000">MX787</font>&quot;,&quot;<font color="#CC0000">A4Q45</font>&quot;};<br>
                                        String[] type = {&quot;<font color="#CC0000">customer</font>&quot;,&quot;<font color="#CC0000">manager</font>&quot;,&quot;<font color="#CC0000">employee</font>&quot;};<br>
                                        String[] desc = {&quot;<font color="#CC0000">Tim@Home</font>&quot;,&quot;<font color="#CC0000">Jack&amp;Moud</font>&quot;,&quot;<font color="#CC0000">John
                                        D'o&eacute;</font>&quot;};<br>
                                        for (int i=0;i&lt;id.length;i++)<br>
                                        {<br>
                                        &nbsp;&nbsp;atts.clear();<br>
                                        &nbsp;&nbsp;atts.addAttribute(&quot;&quot;,&quot;&quot;,&quot;ID&quot;,&quot;CDATA&quot;,id[i]);<br>
                                        &nbsp;&nbsp;atts.addAttribute(&quot;&quot;,&quot;&quot;,&quot;TYPE&quot;,&quot;CDATA&quot;,type[i]);<br>
                                        &nbsp;&nbsp;hd.startElement(&quot;&quot;,&quot;&quot;,&quot;USER&quot;,atts);<br>
                                        &nbsp;&nbsp;hd.characters(desc[i].toCharArray(),0,desc[i].length());<br>
                                        &nbsp;&nbsp;hd.endElement(&quot;&quot;,&quot;&quot;,&quot;USER&quot;);<br>
                                        }<br>
                                        hd.endElement(&quot;&quot;,&quot;&quot;,&quot;USERS&quot;);<br>
                                        hd.endDocument();</font><font face="Verdana, Arial, Helvetica, sans-serif" size="-2"><br>
                                        <font color="#0000FF">[...]</font></font></p>
                                    </td>
                                  </tr>
                                </table>
                                <p><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">This
                                  last sample might be the best solution because
                                  it uses JAXP 1.1 so it will work under JDK 1.4
                                  or JDK 1.2/1.3 with XALAN2 library (or any XML
                                  library JAXP 1.1 compliant). It's also memory-friendly
                                  because it doesn't need DOM.<br>
                                  <br>
                                  Assuming that you're not convinced that you
                                  should use JAXP, think about Servlet Engines
                                  (Tomcat, Resin ...) and Application servers
                                  (Websphere, Weblogic ...) .Most of them already
                                  include an XML parser/processor and try to add
                                  a new one could generate seal errors. In conclusion,
                                  well-formed XML generation is not so easy for
                                  a novice programmer. A smart, standard, portable,
                                  memory-friendly, solution should be based on
                                  JAXP 1.1 + SAX.<br>
                                  <font size="-2"><u>Note</u> : All samples could
                                  be downloaded as a JBuilder projet : <a href="xmlgeneration/xmlsamples.zip">xmlsamples.zip</a></font><br>
                                  <br>
                                  <font size="-2"><u>References</u> : <br>
                                  XML Apache : <a href="http://xml.apache.org">http://xml.apache.org</a><br>
                                  JAXP : <a href="http://java.sun.com/xml/jaxp/index.html">http://java.sun.com/xml/jaxp/index.html</a></font><br>
                                  <br>
                                  ----<br>
                                  07/05/2002,<br>
                                  <a
                                onMouseOver="return overlib('http://www.javazoom.net. We provide Servlets &amp; JSP components, Applets, Open source projects and services.');"
                                onMouseOut="return nd();" target=_blank
                                href="http://www.javazoom.net/">JavaZOOM Team</a></font>
                                </p>
                              </td>
                            </tr>
                          </table>
                          <table width="100%"  border="0" cellspacing="1" cellpadding="0">
                            <tr>
                              <td><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><strong>Learn more</strong> ... search for : <strong>xml parser</strong></font></td>
                            </tr>
                            <tr>
                              <td><!-- Search Google -->
<form method="get" action="http://www.google.com/custom" target="google_window">
<table bgcolor="#ffffff">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
<a href="http://www.google.com/">
<img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a>
<input type="text" name="q" size="32" maxlength="255" value=""></input>
<input type="submit" name="sa" value="Search"></input>
<input type="hidden" name="client" value="pub-6941759278582827"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="ie" value="ISO-8859-1"></input>
<input type="hidden" name="oe" value="ISO-8859-1"></input>
<input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1;"></input>
<input type="hidden" name="hl" value="en"></input>
</td></tr></table>
</form>
<!-- Search Google --></td>
                            </tr>
                          </table>                          </td>
                      </tr>
                    </table>
                  </td>
                </tr>
              </table>
              &nbsp;
            </center>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table width="770" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="146">&nbsp;</td>
    <td width="624" align="center">
      <table border="0" cellspacing="1" cellpadding="1" vspace="0" hspace="0" name="Short Menu" width="100%" align="center" height="30">
        <tr>
          <td nowrap align="center" width="14%" bgcolor="#FFFF99">
            <p>
              <center>
                <font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><a href="../../index.html" target="_self">[News]</a></font>
              </center>
          </td>
          <td nowrap align="center" width="14%" bgcolor="#FFFF99">
            <p>
              <center>
                <font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><a href="../../applets/applets.html" target="_self">[Applets]</a></font>
              </center>
          </td>
          <td nowrap align="center" width="14%" bgcolor="#FFFF99">
            <p>
              <center>
                <font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><a href="../../jzservlets/servlets.html" target="_self">[Servlets]</a></font>
              </center>
          </td>
          <td nowrap align="center" width="16%" bgcolor="#FFFF99">
            <p>
              <center>
                <font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><a href="../services.html"
        target="_self">[Services]</a></font>
              </center>
          </td>
          <td nowrap width="14%" bgcolor="#FFFF99">
            <p>
              <center>
                <font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><a href="../../projects.html">[Projects]</a></font>
              </center>
          </td>
          <td nowrap align="center" width="14%" bgcolor="#FFFF99">
            <p>
              <center>
                <font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><a href="../../links/links.html" target="_self">[Links]</a></font>
              </center>
          </td>
          <td nowrap align="center" width="14%" bgcolor="#FFFF99">
            <p>
              <center>
                <font color="#0000FF" size="-2" face="Arial, Helvetica, sans-serif"><a href="../../links/links.html">[About]</a></font>
              </center>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td width="146" align="center" valign="bottom"></td>
    <td width="624" align="center" valign="bottom" nowrap>
      <p><font size="-1" face="Courier New, Courier, mono">Copyright &copy; JavaZOOM
        1999-2004</font></p>
      <p><font size="-2" face="Arial, Helvetica, sans-serif"><a href="http://java.sun.com" target="_blank" class="style1">Java</a> and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U. S. and other countries.<br />
All other company and/or product names are the property of their respective owners.</font></p>
    </td>
  </tr>
</table>
</body>
</html>
