【问题标题】:Creating an XML document using namespaces in Java在 Java 中使用命名空间创建 XML 文档
【发布时间】:2010-10-06 09:53:19
【问题描述】:

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

【问题讨论】:

    标签: java xml namespaces xmldocument xom


    【解决方案1】:

    有很多方法可以做到这一点。举几个例子:

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

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

    【讨论】:

    • 如果你想要带前缀的元素名称(使用 XOM),只需调用 new Element("prefix:element", "urn:example.namespace");
    【解决方案2】:

    我不确定你想做什么,但我的大部分 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>
    

    其中应该包含您需要的一切。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      • 2012-06-23
      相关资源
      最近更新 更多