[jdom-interest] Parsing an XML file

Soeren Arleth SOEREN.ARLETH at oracle.com
Thu Dec 6 13:32:54 PST 2001


Hi Rajesh,
I got this from the jdom-b7/samples katalog when I downloaded jdom-b7.zip from www.jdom.org - check it out for more examples - the one you have requestet comes here:
regards
Soeren
Code:
*/
package samples;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/**
 * <p><code>SAXBuilderDemo</code> demonstrates how to
 *   build a JDOM <code>Document</code> using a SAX 2.0
 *   parser.
 * </p>
 * 
 * @author Brett McLaughlin
 * @author Jason Hunter
 * @version 1.0
 */
public class SAXBuilderDemo {
    /**
     * <p>
     * This provides a static entry point for creating a JDOM
     *   <code>{@link Document}</code> object using a SAX 2.0
     *   parser (an <code>XMLReader</code> implementation).
     * </p>
     *
     * @param args <code>String[]</code>
     *        <ul>
     *         <li>First argument: filename of XML document to parse</li>
     *         <li>Second argument: optional name of SAX Driver class</li>
     *        </ul>
     */
    public static void main(String[] args) {
        if ((args.length != 1) && (args.length != 2)) {
            System.out.println(
                "Usage: java samples.SAXBuilderTest " +
                "[XML document filename] ([SAX Driver Class])");
            return;
        }
        // Load filename and SAX driver class
        String filename = args[0];
        String saxDriverClass = null;
        if (args.length == 2) {
            saxDriverClass = args[1];
        }
        // Create an instance of the tester and test
        try {
            SAXBuilder builder = null;
            if (saxDriverClass == null) {
                builder = new SAXBuilder();
            } else {
                builder = new SAXBuilder(saxDriverClass);
            }
            Document doc = builder.build(filename);
            // Create an outputter
            XMLOutputter outputter = new XMLOutputter();
            //outputter.setTrimText(true);
            //outputter.setExpandEmptyElements(true);
            outputter.output(doc, System.out);
        } catch (JDOMException e) {
            if (e.getRootCause() != null) {
                e.getRootCause().printStackTrace();
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }
}








___________________________________

Soeren Arleth - Oracle Denmark

EMEA Java/XML Professional Community

"Build a better mousetrap, and the world will

beat a path to your door." 

--Emerson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20011206/2c741e94/attachment.htm


More information about the jdom-interest mailing list