Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an XML document using namespaces in Java

I am looking for example Java code that can construct an XML document that uses namespaces. I cannot seem to find anything using my normal favourite tool so was hoping someone may be able to help me out.

like image 720
adam Avatar asked Feb 09 '09 14:02

adam


People also ask

How do you create a namespace in XML?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is namespace used in XML document?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.


1 Answers

There are a number of ways of doing this. Just a couple of examples:

Using XOM

import nu.xom.Document;
import nu.xom.Element;

public class XomTest {

    public static void main(String[] args) {
        XomTest xomTest = new XomTest();
        xomTest.testXmlDocumentWithNamespaces();
    }

    private void testXmlDocumentWithNamespaces() {
        Element root = new Element("my:example", "urn:example.namespace");
        Document document = new Document(root);
        Element element = new Element("element", "http://another.namespace");
        root.appendChild(element);
        System.out.print(document.toXML());
    }
}

Using Java Implementation of W3C DOM

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class DomTest {

    private static DocumentBuilderFactory dbf = DocumentBuilderFactory
            .newInstance();

    public static void main(String[] args) throws Exception {
        DomTest domTest = new DomTest();
        domTest.testXmlDocumentWithNamespaces();
    }

    public void testXmlDocumentWithNamespaces() throws Exception {
        DocumentBuilder db = dbf.newDocumentBuilder();
        DOMImplementation domImpl = db.getDOMImplementation();
        Document document = buildExampleDocumentWithNamespaces(domImpl);
        serialize(domImpl, document);
    }

    private Document buildExampleDocumentWithNamespaces(
            DOMImplementation domImpl) {
        Document document = domImpl.createDocument("urn:example.namespace",
                "my:example", null);
        Element element = document.createElementNS("http://another.namespace",
                "element");
        document.getDocumentElement().appendChild(element);
        return document;
    }

    private void serialize(DOMImplementation domImpl, Document document) {
        DOMImplementationLS ls = (DOMImplementationLS) domImpl;
        LSSerializer lss = ls.createLSSerializer();
        LSOutput lso = ls.createLSOutput();
        lso.setByteStream(System.out);
        lss.write(document, lso);
    }
}
like image 132
toolkit Avatar answered Sep 26 '22 07:09

toolkit