Difference between revisions of "Load a XML document"

From CodeCodex

(Java)
 
Line 3: Line 3:
 
{{LibraryRequired|url=http://www.jdom.org/|name=JDOM}}
 
{{LibraryRequired|url=http://www.jdom.org/|name=JDOM}}
 
<pre>
 
<pre>
 +
import org.jdom.*;
 +
import org.jdom.input.*;
 +
import java.io.*;
 +
 
/**
 
/**
 
  * Loads a JDOM XML Document
 
  * Loads a JDOM XML Document
Line 21: Line 25:
 
}
 
}
 
</pre>
 
</pre>
 +
 
===OCaml===
 
===OCaml===
 
{{LibraryRequired|url=http://tech.motion-twin.com/xmllight.html|name=XML-Light}}
 
{{LibraryRequired|url=http://tech.motion-twin.com/xmllight.html|name=XML-Light}}

Latest revision as of 19:28, 18 August 2006

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