首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Java中使用名称空间创建XML文档

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

Stack Overflow用户
提问于 2009-02-09 14:22:48
回答 2查看 58.1K关注 0票数 18

我正在寻找可以构造使用名称空间的XML文档的示例Java代码。我似乎找不到任何东西,使用我的正常favourite tool,所以希望有人可以帮助我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-02-09 15:04:05

我不确定您想要做什么,但是我使用jdom来解决我的大多数xml问题,并且它(当然)支持名称空间。

代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>

里面应该有你需要的所有东西。希望这能有所帮助。

票数 16
EN

Stack Overflow用户

发布于 2009-02-09 15:05:40

有许多方法可以做到这一点。这里只有几个例子:

使用XOM

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}
票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/528312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档