使用Java创建带命名空间的XML文档

18

我正在寻找示例Java代码,该代码可以构造使用命名空间的XML文档。我使用我的常用搜索工具似乎找不到任何内容,因此希望有人能够帮助我。

2个回答

22

有多种方法可以实现这个目标。以下是一些示例:

使用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());
    }
}

使用Java实现的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);
    }
}

1
如果您想要使用前缀(使用XOM)获取元素名称,只需调用new Element("prefix:element", "urn:example.namespace")。 - Peter Štibraný

16

我不确定你想做什么,但是我在大多数XML问题上使用jdom,它当然支持命名空间。

代码:

Document doc = new Document();
Namespace sNS = Namespace.getNamespace("someNS", "someNamespace");
Element element = new Element("SomeElement", sNS);
element.setAttribute("someKey", "someValue", Namespace.getNamespace("someONS", "someOtherNamespace"));
Element element2 = new Element("SomeElement", Namespace.getNamespace("someNS", "someNamespace"));
element2.setAttribute("someKey", "someValue", sNS);
element.addContent(element2);
doc.addContent(element);

生成以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
 <someNS:SomeElement xmlns:someNS="someNamespace" xmlns:someONS="someOtherNamespace"  someONS:someKey="someValue">
  <someNS:SomeElement someNS:someKey="someValue" />
 </someNS:SomeElement>

它应该包含你所需的一切。希望能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接