2

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.addNamespaceDeclaration(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

1
  • You have declared a namespace without a prefix - that's what xmlns="url" is doing. Commented Aug 16, 2011 at 22:01

2 Answers 2

9

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.