开发者

Set namespace using jdom

I would like have this format in xml:

<ns2: test xmlns="url" xmlns:ns2="url2" xmlns:ns3="url3">
....
</ns2: test>

I am using the following code:

    Namespace ns= Namespace.getNamespace("url");
    Namespace ns2 = Namespace.getNamespace("ns2", "url2");
    Namespace ns3= Namespace.getNamespace("ns3", "url3");

    SAXBuilder vDocBuilder = new SAXBuilder();
    Document vDocument = vDocBuilder.build(File);

    System.out.println("Root element " + vDocument.getRootElement().getName());

    org.jdom.Element test = new org.jdom.Element("test", ns);
    vDocument.setRootElement(test);
    vNewRootElement.addNamespaceDeclaration(ns2);
    vNewRootElement.addNamespaceDeclarat开发者_C百科ion(ns3);

If I set namespace with:

    vNewRootElement.setNamespace(ns3);

Then I get thi:s

    <ns2: test xmlns:ns2="url2" xmlns:ns3="url3"> ... </ns2: test> 
without the default namespace xmlns="url".

Can anybody tell me why it dosen't work and is there a way to solve this problem?

Thanks, haner


The following outputs (to System.out)

<?xml version="1.0" encoding="UTF-8"?>
<ns2:test xmlns:ns2="url2" xmlns="url" xmlns:ns3="url3">Some text</ns2:test>

import java.io.IOException;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.output.XMLOutputter;

public class Test {

    public static void main(String args[]) throws JDOMException, IOException {
        Namespace ns = Namespace.getNamespace("url");
        Namespace ns2 = Namespace.getNamespace("ns2", "url2");
        Namespace ns3 = Namespace.getNamespace("ns3", "url3");


        Document vDocument = new Document();
        org.jdom.Element test = new org.jdom.Element("test", ns2);
        vDocument.setRootElement(test);
        //add "url" default namespace
        test.addNamespaceDeclaration(ns);
        test.addNamespaceDeclaration(ns2);
        test.addNamespaceDeclaration(ns3);
        test.addContent("Some text");
        //dump output to System.out
        XMLOutputter xo = new XMLOutputter();
        xo.output(vDocument, System.out);

    }
}

you need the line

   test.addNamespaceDeclaration(ns);


Add the namespace like this:

val root = new Element("test", "url")

this will output:

<test xmlns="url">

if you want child elements to also use the same namespace, do this when adding the element:

val msgHeader = new Element("body", test.getNamespace)

otherwise, the child element will have xmlns="" by default.

Sample code in Scala:

import org.jdom.output.{Format, XMLOutputter}
import org.jdom.{Document, Element, Namespace}

val doc = new Document()

//Root Element
val root = new Element("test", "url")
val ns2 = Namespace.getNamespace("ns2", "url2")

root.addNamespaceDeclaration(ns2)
root.setAttribute("attribute1", "value1")

val xmlHeader = new Element("MessageHeader", root.getNamespace)
val xmlBody = new Element("MessageBody", root.getNamespace)

root.addContent(xmlHeader)
root.addContent(msgBody)

doc.setRootElement(root)

this will output:

<test xmlns="url" xmlns:ns2="url2" attribute1="value1">
  <MessageHeader>  </MessageHeader>
  <StatusMessageBody> </StatusMessageBody>
</test>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜