在 Java 中使用命名空间创建 XML 文档

发布于 2024-07-13 13:05:55 字数 133 浏览 4 评论 0原文

我正在寻找可以构造使用命名空间的 XML 文档的示例 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-07-20 13:05:56

有多种方法可以做到这一点。 仅举几个示例:

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

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

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);
    }
}
轻拂→两袖风尘 2024-07-20 13:05:56

我不确定你想做什么,但我使用 jdom 来处理我的大多数 xml 问题,它支持命名空间(当然)。

代码:

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>

其中应该包含您需要的所有内容。 希望有帮助。

I am not sure, what you trying to do, but I use jdom for most of my xml-issues and it supports namespaces (of course).

The code:

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

produces the following 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>

Which should contain everything you need. Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文