One strategy for generating an XML document is to build an XML parse tree and then use JDOM methods to output the XML string for that tree.. This approach is often preferable for large, complex structures, especially those in which recursive elements are present or those that involve conditional logic.Another use is for updating an existing XML document or building it in successive stages, as with a succession of filters or processing stages.
JDOM includes classes and methods that make this a relatively simple process. In the example below, the XML for a simple login form is generated by first building a parse tree and then outputting the xml for that tree. To show the difference between this approach and generating the XML as a concatenated String, the statements for the latter are shown, but commented-out.
packagejbs.ab3.view;importjava.io.IOException; import java.io.OutputStream; import java.io.PrintWriter;importjava.util.List; import java.util.Vector;importjavax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;importorg.jdom.Comment; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXHandler; import org.jdom.output.XMLOutputter; import jbs.ab3.databean.BeanLogin;publicclass ViewLogin {public static void display( HttpServletRequest req, HttpServletResponse resp, BeanLogin bean ) throws IOException {resp.setContentType("text/html"); ServletOutputStream out = resp.getOutputStream();SAXHandler handler = new SAXHandler();// start the tree Document document = handler.getDocument();// build the root Element root = new Element( "view" );root.setAttribute( "type", "login" ); document.setRootElement( root );// build root children Vector rootChildren = new Vector(); Element heading = new Element( "heading" ); heading.addContent( "Please Login to AB3" ); rootChildren.add( heading );Element content = new Element( "content" ); rootChildren.add( content );Element message = new Element( "message" ); message.addContent( bean.getMessage() ); rootChildren.add( message );root.setContent( rootChildren );// build content children Vector contentChildren = new Vector();Element state = new Element( "state" ); contentChildren.add( state );Element entry = new Element( "entry" ); contentChildren.add( entry );content.setContent( contentChildren );// build entry children Vector entryChildren = new Vector();Element itemLogin = new Element( "item" ); Vector itemLoginChildren = new Vector(); itemLoginChildren.add( new Element( "label" ).addContent("Login") ); itemLoginChildren.add( new Element( "name" ).addContent("loginName") ); itemLoginChildren.add( new Element( "value" ).addContent(bean.getLoginName()) ); itemLogin.setContent( itemLoginChildren );entryChildren.add( itemLogin ); Element itemPassword = new Element( "item" ); Vector itemPasswordChildren = new Vector(); itemPassword.setAttribute( "type", "password" ); itemPasswordChildren.add( new Element( "label" ).addContent("Password") ); itemPasswordChildren.add( new Element( "name" ).addContent("loginPassword") ); itemPasswordChildren.add( new Element( "value" ).addContent(bean.getLoginPassword()) ); itemPassword.setContent( itemPasswordChildren );entryChildren.add( itemPassword );entry.setContent( entryChildren );XMLOutputter outputter = new XMLOutputter(); String xml = outputter.outputString( document );//ViewJdom.parse( xml );/*String xml = "<?xml version='1.0' encoding='UTF-8'?>";xml += "<view type='login'>"; xml += "<heading>"; xml += "Please Login to AB3"; xml += "</heading>"; xml += "<content>"; xml += "<state>"; xml += "</state>"; xml += "<entry>"; xml += "<item>"; xml += "<label>Login</label>"; xml += "<name>loginName</name>"; xml += "<value>" + bean.getLoginName() + "</value>"; xml += "</item>"; xml += "<item type='password'>"; xml += "<label>Password</label>"; xml += "<name>loginPassword</name>"; xml += "<value>" + bean.getLoginPassword() + "</value>"; xml += "</item>"; xml += "</entry>"; xml += "</content>";xml += "<message>" + bean.getMessage() + "</message>";xml += "</view>";
*/
String xsl = "http://"; xsl += req.getServerName() + ":"; xsl += req.getServerPort(); xsl += View.XSL_FILE;
View.transform ( xml, xsl, out );
out.close();
}
}
<?xml version="1.0" encoding="UTF-8"?> <view type="login"><heading>Please Login to AB3</heading> <content><state /><entry><item><label>Login</label><name>loginName</name> <value>111</value></item><item type="password"><label>Password</label> <name>loginPassword</name><value /></item></entry></content> <message>Generated Parse tree and build XML from it.</message></view>
<?xml version="1.0" encoding="UTF-8"?> <view type="login"> <heading>Please Login to AB3</heading> <content> <state /> <entry> <item> <label>Login</label> <name>loginName</name> <value>111</value> </item> <item type="password"> <label>Password</label> <name>loginPassword</name> <value /> </item> </entry> </content> <message>Generated Parse tree and build XML from it.</message> </view>