[jdom-interest] A question regarding XML transformation to Plain text using XSLT and JDOM

Bradley S. Huffman hip at cs.okstate.edu
Thu Aug 14 09:37:00 PDT 2003


XMLOutputter has a output method that takes a list of nodes and your 
transformDocument could just return the List from out.getResult().

But IMHO a better solution in this case would be to use StreamResult (in
package javax.xml.transform.stream) instead of JDOMResult.

Brad

"Pradeep Gummi" writes:

> Hi
> I would like to transform an XML document to plain text format (having
> no   root element, just plain text) and I am using the JDOM API, xml
> transform package for it. I am successfully able to trasnform the
> document  but to other XML format and not plain text since I am using
> XMLOutputter class for it. And this would require creation of a root
> element. So, I would like to know what outputter should I be using so as
> to make the output just plain text having no root.
> Any suggestions, 
> thanking you
> pradeep gummi
> 
> Check the following code:
> **************************************************** 
>     import org.jdom.transform.*;
>     import org.jdom.*;
>     import javax.xml.transform.*;
>     import javax.xml.transform.stream.*;
>     import org.jdom.output.*;
> 
> class YYYY{
> ....
> //method//
> public static void main(String args[]){
>    Document abDoc = ab.getDocument();
>      boolean isHTMLEmail = ab.isUserHTMLEmail();
>      System.out.println("is HTML EMAIL " + isHTMLEmail);
> 	      File outputFile = new File("Announcement.dat");
> 	StringBuffer message1 = new StringBuffer();
>    Document outputDocument = transformDocument(isHTMLEmail, abDoc);
> DataOutputStream dos=new DataOutputStream(new FileOutputStream(outputFile));
> 	try {
> 		  XMLOutputter serializer = new XMLOutputter();
> 		  serializer.setOmitDeclaration(true);
> 		  serializer.setIndent("  "); // use two space indent
> 		  serializer.setNewlines(true); 
> 		  serializer.output(outputDocument,dos);
> 		}
> 		catch (IOException e) {
> 		  System.err.println(e);
> 	}
> }
> 
> public static Document transformDocument(boolean isHTMLEmail, Document
> doc) throws JDOMException{
> 			Document resultDoc = new Document();
> 			JDOMSource js = new JDOMSource(doc);
> 			StreamSource styleSheetSource;
> 			try {
> 	// source of the style sheet
> styleSource= new StreamSource(this.textsheet);
> //Create the Transformer
> TransformerFactory transformerFactory= TransformerFactory.newInstance();
> Transformer transformer =TransformerFactory.newTransformer(styleSource)
> // Document object of the result
> 	JDOMResult out = new JDOMResult();
> //Transform the document to output
> 	 transformer.transform(js, out);
> 	List resultList = out.getResult();
>         out.setResult(resultList);
> 	return out.getDocument();
>  }
> }



More information about the jdom-interest mailing list