本文介紹了在 Java 中使用命名空間創(chuàng)建 XML 文檔的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在尋找可以構(gòu)建使用名稱空間的 XML 文檔的示例 Java 代碼.我似乎無(wú)法使用我的常規(guī)最喜歡的工具找到任何東西,所以希望有人能幫助我.p>
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.
推薦答案
我不確定,你想做什么,但我使用 jdom 用于我的大多數(shù) xml 問(wèn)題,它支持命名空間(當(dāng)然).
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).
代碼:
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>
其中應(yīng)該包含您需要的一切.希望對(duì)您有所幫助.
Which should contain everything you need. Hope that helps.
這篇關(guān)于在 Java 中使用命名空間創(chuàng)建 XML 文檔的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!