Header javaperspective.com
JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.5. Outputting XML from Java code

3.5. Outputting XML from Java code
Last updated: 9 February 2013.

This tutorial will show you how to output a JDOM document to the standard output, to a file and to a String object.


3.5.1. How to output a JDOM document to the standard output

The class XMLOutputter provides several output methods. As an example, the code below shows how to print to the standard output an XML document previously obtained by reading the XML file hotel.xml you may have already seen in the tutorial What is XML:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void readAndOutput(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Output the JDOM document to the standard output
               
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                outputter.output
(document, System.out);
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readAndOutput();
   
}

}

Here is the result:

   <?xml version="1.0"?>
   <HOTEL>
            <ROOM>
                     <NUMBER>17</NUMBER>
                     <CATEGORY>STANDARD</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <FLOOR>5</FLOOR>
                     <LIVING_ROOM/>
                     <KITCHENETTE/>
                     <AIR_CONDITIONING>
                              <TABLE_FANS>2</TABLE_FANS>
                     </AIR_CONDITIONING>
            </ROOM>
            <ROOM>
                     <NUMBER>33</NUMBER>
                     <CATEGORY>SUITE</CATEGORY>
                     <BED>DOUBLE SIZE</BED>
                     <BED>KING SIZE</BED>
                     <FLOOR>9</FLOOR>
                     <LIVING_ROOM>17 FEET * 13 FEET</LIVING_ROOM>
                     <KITCHENETTE>10 FEET * 8 FEET</KITCHENETTE>
                     <JACUZZI>3 SEATS</JACUZZI>
                     <AIR_CONDITIONING>
                              <AIR_CONDITIONERS>5</AIR_CONDITIONERS>
                     </AIR_CONDITIONING>
            </ROOM>
   </HOTEL>

The class XMLOutputter provides several constructors. The one I have just used takes a Format argument whose purpose is to specify a given output format. The possible options are:

3.5.2. How to output a JDOM document to a file

To output a JDOM document to a file, just use one of the following methods provided by the class XMLOutputter:Here is an example:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void readAndOutput(){
         
BufferedWriter writer = null;

         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Create a BufferedWriter object
               
writer = new BufferedWriter(new FileWriter("copyOfHotel.xml"));

               
// Output the JDOM document to the BufferedWriter object
               
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                outputter.output
(document, writer);
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
         
finally{
               
if(writer != null){
                     
try {
                           
writer.close();
                     
}
                     
catch (IOException e) {
                           
e.printStackTrace();
                     
}
                }
          }
    }


   
public static void main(String[] args){
         
new JDOMDemo().readAndOutput();
   
}

}

By default, the encoding used by the class XMLOutputter is UTF-8. If you want another encoding, use the method setEncoding provided by the class Format as follows:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void readAndOutput(){
         
BufferedWriter writer = null;

         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Create a Format object and set the encoding
               
Format format = Format.getPrettyFormat();
                format.setEncoding
("ISO-8859-1");

               
// Create a BufferedWriter object with an OutputStreamWriter that uses the same encoding
                // The output target must be an output stream with the encoding set as well
               
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copyOfHotel.xml"), "ISO-8859-1"));

               
// Output the JDOM document to the BufferedWriter object
               
XMLOutputter outputter = new XMLOutputter(format);
                outputter.output
(document, writer);
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
         
finally{
               
if(writer != null){
                     
try {
                           
writer.close();
                     
}
                     
catch (IOException e) {
                           
e.printStackTrace();
                     
}
                }
          }
    }


   
public static void main(String[] args){
         
new JDOMDemo().readAndOutput();
   
}

}

The encoding will be added to the prolog of the file copyOfHotel.xml as shown below:

<?xml version="1.0" encoding="ISO-8859-1"?>



3.5.3. How to output a JDOM document to a string

Use the method ouputString provided by the class XMLOutputter. The code follows:

import java.io.File;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public final class JDOMDemo {


   
private void readAndOutput(){
         
try {
               
// Read the XML file and get a JDOM document
               
SAXBuilder builder = new SAXBuilder();
                Document document = builder.build
(new File("hotel.xml"));

               
// Output the JDOM document to the String object
               
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
                String xml = outputter.outputString
(document);

               
// Do something with xml
                // ...
         
}
         
catch (Exception e) {
               
e.printStackTrace();
         
}
    }


   
public static void main(String[] args){
         
new JDOMDemo().readAndOutput();
   
}

}


You are here :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.5. Outputting XML from Java code
Next tutorial :  JavaPerspective.com  >   Advanced Tutorials  >   3. XML processing with JDOM  >   3.6. Adding-Editing-Removing nodes

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7