Adding XML namespaces via Dom4j


Hello everyone,

today’s article is about adding namespaces to your XML document using Java and Dom4j, so let’s get started…

First of all we need an instance of the Dom4j factory, here is how we do it…

//Get an instance of the dom4j document factory
DocumentFactory factory = DocumentFactory.getInstance();

…next we create an element which is going to serve as the root element as well as a new xml document using the factory object.

//use the factory to create a root element
Element rootElement = factory.createElement("RootElement");
//use the factory to create a new document with the previously created root element
Document doc = factory.createDocument(rootElement);

Now we are ready to create our namespaces.

//create some dom4j namespaces that we like to add to our new document
Namespace namespace1 = new Namespace("xsd","http://www.w3.org/2001/XMLSchema#");
Namespace namespace2 = new Namespace("rdfs","http://www.w3.org/2000/01/rdf-schema#");
Namespace namespace3 = new Namespace("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#");
Namespace namespace4 = new Namespace("owl","http://www.w3.org/2002/07/owl#");
Namespace namespace5 = new Namespace("some","http://some/other/namespace:-)#");

The last step we have to take is to add all the namespaces to the XML document.

//add the created namespaces to the document</span>
doc.getRootElement().add(namespace1);
doc.getRootElement().add(namespace2);
doc.getRootElement().add(namespace3);
doc.getRootElement().add(namespace4);
doc.getRootElement().add(namespace5);

Finished! To check if everything worked as intended we’re going to write the document to an XML file…

try{
  //write the created document to an arbitrary file
  FileOutputStream fos = new FileOutputStream( "files/output.xml" );

  OutputFormat outformat = OutputFormat.createPrettyPrint();
  XMLWriter writer = new XMLWriter(fos, outformat);
  writer.write(doc);
  writer.flush();

}catch(FileNotFoundException e) {
  // catch exception
  e.printStackTrace();
}catch(UnsupportedEncodingException e) {
  // catch exception
  e.printStackTrace();
}catch (IOException e) {
  // catch exception
  e.printStackTrace();
}

…if everything went fine we should end up with an XML file that looks like this:

<xml version="1.0" encoding="UTF-8">

<RootElement xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:some="http://some/other/namespace:-)#">

</RootElement>

So this is how you add namespaces to your xml document via Dom4j…have a nice day and stay tuned for more face-melting articles :)…

Advertisements

2 comments

  1. This is certainly the 4th blog, of yours I read.
    Still I personally enjoy this specific 1, “Adding XML namespaces via Dom4j Software Engineering Orkus”
    the best. Cya ,Shanna


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s