Load a XML document

From CodeCodex

Revision as of 19:28, 18 August 2006 by (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)




This code requires JDOM, an external library, to run.
import org.jdom.*;
import org.jdom.input.*;
import java.io.*;

 * Loads a JDOM XML Document
 * @param f The file representing the Document
 * @return The loaded Document or null if file was not well-formed XML or not found
public static Document loadXml(File f) {	
	Document d = new Document();
	SAXBuilder saxb = new SAXBuilder();
	try {
		d = saxb.build(f);
	} catch (Exception e) {
		System.out.println("Error loading "+ f.toString() +". Error: "+e.getMessage());
		return null;
	return d;



This code requires XML-Light, an external library, to run.

This is a one-liner in OCaml:

# Xml.parse_file "test.xml";;
- : Xml.xml = Xml.Element ("foo", [], [Xml.PCData "bar"])