Listing  1   private void getXMLFile() & private void postData()

private void getXMLFile()
   {
       JFileChooser fileChooser = new JFileChooser();
       fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
       int result = fileChooser.showOpenDialog(this);
       if(result == JFileChooser.CANCEL_OPTION)
           xmlFile = null;
       else
          xmlFile = fileChooser.getSelectedFile();
   }
   private void postData()
  {
       try
       {
           Document document = null;
           DOMBuilder builder = new DOMBuilder(false);
           document = builder.build(xmlFile);
           if(document != null)
           {
               postURL = urlField.getText();
               URL url = new URL(postURL);
               System.out.println("after new URL(...");
               HttpURLConnection urlCon =
                (HttpURLConnection)url.openConnection();
               urlCon.setDoOutput(true);
               OutputStreamWriter out = new
                OutputStreamWriter(urlCon.getOutputStream());
               XMLOutputter outputter = new XMLOutputter("\t",true);
               outputter.output(document, out);
               out.flush();
               StringBuffer sb = new StringBuffer();
               InputStream in = urlCon.getInputStream();
               BufferedReader br = new BufferedReader
                (new InputStreamReader(in));
               String lineOfText;
               try
               {
                   while((lineOfText = br.readLine()) != null)
                   {
                       sb.append(lineOfText);
                   }
               }
               catch(Exception e)
               {
                   textArea.append(e.toString());
               }
               textArea.append(sb.toString());
           }
       }
       catch(Exception e)
       {
           System.out.println(e.toString());
       }
   }


Listing  2   doPost method in servlet


public void doPost(HttpServletRequest request,
    HttpServletResponse response)
       throws ServletException, IOException
   {
     PrintWriter out = new PrintWriter
      (response.getOutputStream());
     DOMBuilder builder = new DOMBuilder(false);
     Document outgoingDocument = null;
     try
     {
       InputStream inputStream =  (java.io.InputStream)
        request.getInputStream();
       try
       {
         /*
                   replace this code with code that calls your
                    application
                    specific code, perhaps an app controller.
                    outgoingDocument =
                    messageController.handleMessage(inputStream);
         */
         Document document = builder.build(inputStream);
         Element root = document.getRootElement();
         Element newRoot = root;
         Element element = new Element("SERVER_TIME");
         element.addContent(new java.util.Date().toString());
         newRoot.addContent(element);
         outgoingDocument = new Document(newRoot);
       }
       catch(org.jdom.JDOMException jdomException)
       {
         outgoingDocument = buildErrorXMLDocument
          (jdomException.getMessage());
       }
     }
     catch(Exception e)
     {
       outgoingDocument = buildErrorXMLDocument(e.getMessage());
     }
     finally
     {
       response.setContentType("text/xml; charset=UTF-8" );
       XMLOutputter outputter = new XMLOutputter("\t",true);
       outputter.output(outgoingDocument, out);
       out.flush();
       out.close();
     }

   }




Listing 1

01: private Object deserializeFromPacket(String packet)
02: throws Exception
03: {
04: String parser = null;
05: Object obj = null;
06: InputSource source = null;
07: WddxDeserializer deserializer = null;
08:
09: // check incoming argument
10: if(packet == null || packet.length() == 0)
11: {
12: // throw error
13: throw new Exception("The incoming packet is NULL or empty.");
14: }
15:
16: // TODO:
17: // setup configuration code for retrieving other possible SAX parsers
18:
19: // set parser value to internal
20: if(parser == null)
21: {
22: // use default parser
23: parser = this.DEFAULT_SAXPARSER;
24: }
25:
26: // attempt to load the parser for verification it is in CLASSPATH
27: try
28: {
29: Class.forName(parser).newInstance();
30: }
31: catch(Exception e)
32: {
33: throw new Exception("Unable to load SAX parser class.
The system was unable to load the required SAX parser classes: " + parser + ".
This might be because it are not in the ColdFusion Server's Java CLASSPATH setting.
"); 34: }
35:
36: // create input source for the XML parser
37: source = new InputSource(new StringReader(packet));
38:
39: // create a WDDX deserializer
40: deserializer = new WddxDeserializer(parser);
41:
42: // deserialize the WDDX packet
43: obj = deserializer.deserialize(source);
44:
45: return obj;
46: }

Listing 2

01: private String serializeToPacket(Object obj)
02: throws Exception
03: {
04: String parser = null;
05: WddxSerializer serializer = new WddxSerializer();
06: StringWriter buffer = new StringWriter();
07:
08: // check incoming argument
09: if(obj == null)
10: {
11: // throw error
12: throw new Exception("The incoming object is null.");
13: }
14:
15: // serialize object
16: serializer.serialize(obj, buffer);
17:
18: return buffer.toString();
19: }

Listing 3

01: <cfparam name="url.sheettype" default="html">
02: <cfset stylesheet =
GetDirectoryFromPath(GetBaseTemplatePath()) &
"booklist.#url.sheettype#.xsl">
03: <cfset booklist =
GetDirectoryFromPath(GetBaseTemplatePath()) &
"booklist.txt">
04: <cffile action="READ" file="#booklist#"
variable="books">
05: <cf_transformation stylesheet="#stylesheet#">
06: <cfoutput>
07: <booklist>
08: <cfloop index="idx" list="#books#">
09: <book>
10: <name>#ListGetAt(idx, 1, "|")#</name>
11: <link>#ListGetAt(idx, 2, "|")#</link>
12: </book>
13: </cfloop>
14: </booklist>
15: </cfoutput>
16: </cf_transformation>

Listing 4

// grab attribute collection
attribAttributesCollection =
request.getAttribute("ATTRIBUTESCOLLECTION", null);
if(attribAttributesCollection == null)
{
throw new Exception("Error! Missing required attribute:
ATTRIBUTESCOLLECTION");
}
mAttributesCollection =
(Hashtable)deserializeFromPacket(attribAttributesCollection);

// retrieve/default the incoming argument
attribStylesheet =
(String)mAttributesCollection.get("STYLESHEET");
attribSource =
(String)mAttributesCollection.get("SOURCE");
attribContent = (String)mAttributesCollection.get
("CONTENT");
attribResult =
(String)mAttributesCollection.get("RESULT");

Additional Code for this Article (~zip file) Strande0303.zip