में नेमस्पेस का उपयोग करके एक XML दस्तावेज़ बनाना I उदाहरण के लिए जावा कोड की तलाश है जो नामस्थान का उपयोग करने वाले XML दस्तावेज़ का निर्माण कर सकता है। मुझे अपने सामान्य favourite tool का उपयोग करके कुछ भी नहीं मिल रहा है, इसलिए उम्मीद है कि कोई मेरी मदद करने में सक्षम हो सकता है।जावा
Q
जावा
18
A
उत्तर
14
मुझे यकीन नहीं है कि आप क्या करने का प्रयास कर रहे हैं, लेकिन मैं अपने अधिकांश 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>
कौन सा सब कुछ आप की जरूरत होनी चाहिए। उम्मीद है की वो मदद करदे।
20
ऐसा करने के कई तरीके हैं। बस कुछ उदाहरणों की:
का उपयोग 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);
}
}
के जावा कार्यान्वयन का उपयोग करना और अगर आप उपसर्ग के साथ तत्व नाम चाहते हैं (XOM का प्रयोग करके), बस नए तत्व ("उपसर्ग फोन : तत्व "," urn: example.namespace "); –