Load a XML document

From CodeCodex

Implementations[edit]

Java[edit]

40px

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;
}

OCaml[edit]

40px

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"])