Namespace prefix in created DOM document string

Hello world,
I am creating a DOM document using the standard JAXP APIs. The document is not parsed from anywhere, just created using the DOM API factory methods. This document is a SOAP message with the SOAP envelope, header and body elements. Now I need to have this document transformed to an XML string.
I can manage all this. However, I want to have the SOAP namespace set for the SOAP elements, and I want it set using a prefix (such as "SOAP-ENV" or "env" as in the example below). I have set the envelope, header and body element namespaces to the soap namespace. The rest of the elements inside header and body I have set to null namespace. I want them without any prefix (and what namespace will that be then, default?).
The problem is, when I transform the document to a string, the namespace is set on the envelope without a prefix, and not on the header or body. I guess this is because the child elements will all inherit the namespace? I tried with xalan and saxon.
Here is an example of output that would look like what I want:
<env:Envelope xmlns:env="http://www.w3.org/2002/12/soap-envelope">
<env:Header>
  <foo>hello</foo>
</env:Header>
<env:Body>
   <bar>is open</bar>
</env:Body>
</env:Envelope>Here is an example of output that looks like what I got:
<Envelope xmlns="http://www.w3.org/2002/12/soap-envelope">
<Header>
  <foo>hello</foo>
</Header>
<Body>
   <bar>is open</bar>
</body>
</Envelope>So so what am I doing wrong, how should I do it? If my rambling makes no sense, even an example of constructing output like the example of what I want would solve this.. :)
Thanks,

You could just create the an attribute like this:
root.setAttribute("xmlns:env", "http://www.w3.org/2002/12/soap-envelope"); where root is the Envelope element.
Hello world,
I am creating a DOM document using the standard JAXP
APIs. The document is not parsed from anywhere, just
created using the DOM API factory methods. This
document is a SOAP message with the SOAP envelope,
header and body elements. Now I need to have this
document transformed to an XML string.
I can manage all this. However, I want to have the
SOAP namespace set for the SOAP elements, and I want
it set using a prefix (such as "SOAP-ENV" or "env" as
in the example below). I have set the envelope,
header and body element namespaces to the soap
namespace. The rest of the elements inside header and
body I have set to null namespace. I want them
without any prefix (and what namespace will that be
then, default?).
The problem is, when I transform the document to a
string, the namespace is set on the envelope without
a prefix, and not on the header or body. I guess this
is because the child elements will all inherit the
namespace? I tried with xalan and saxon.
Here is an example of output that would look like
what I want:
<env:Envelope
xmlns:env="http://www.w3.org/2002/12/soap-envelope">
<env:Header>
<foo>hello</foo>
</env:Header>
<env:Body>
<bar>is open</bar>
</env:Body>
</env:Envelope>Here is an example of output that looks like what I
got:
<Envelope
xmlns="http://www.w3.org/2002/12/soap-envelope">
<Header>
<foo>hello</foo>
</Header>
<Body>
<bar>is open</bar>
</body>
</Envelope>So so what am I doing wrong, how should I do it? If my
rambling makes no sense, even an example of
constructing output like the example of what I want
would solve this.. :)
Thanks,

Similar Messages

  • Creating DOM Document with elements

    I want to recreate a new document, I am wondering if there is a way to create one using only elements, so I dont have to go through the whole document and appendchild(), or if there are any other ways of doing this.

    Thnx for the quick responses. I do not want to simply recreate the document, but I have modified the elements and I wish to create a instance of the modified document. I try to populate the data here:
            for (int i = 0; i <= ((int)biggestID); i++)
            {   if (allTreeItems[i] != null)
                    if (allTreeItems.getParent() == null)
    rootIndex = i;
    if (allTreeItems[i].getParent() != null)
    parentIndex = (int)(((TreeItem)allTreeItems[i].getParent()).getItemID());
    allTreeItems[parentIndex].add(allTreeItems[i]);
    try{
    (allTreeItems[parentIndex].getElement()).appendChild(allTreeItems[i].getElement());
    }catch (DOMException dome) {JOptionPane.showInputDialog("Somethen aint rite");}
    return allTreeItems[rootIndex].getElement();
    and return the root element and use getOwnerDocument() to get the new document but it returns the original document. Any ideas on my options here?

  • Setting namespace prefix with JAXB

    I'm trying to build and use a JAXB application.
    1. Generate the Java source files by submitting the XML Schema with Namespace to the binding compiler.
    2. Compile the Java source code.
    3. Marshal the data to new XML documents(valid XML document).
    But namespace prefix of XML Schema doesn't equal namespace prefix of new XML document.
    The prefix of new XML Schema is "heo". But, the prefix of new XML document set as "ns1" at discretion.
    Is there any way to solve namespace prefix setting?
    Thank you for your help.

    Why is java the only one doing this?
    I know prefixes are arbitrary, but when groups/individuals design XML docs, the instinct is to specify unique prefixes and to expect them coming in and out of other systems.
    When joe-developer emails an API to a buddy, she's got to include a lot more info every time, not just "cool:news" vs. "bad:news" but "cool:news" and "xmlns:cool=uri-foobar-smyte" and ...
    Sooo much simpler to handle the xml when its not only declared correctly, but you can expect specific prefixes. Less lines in writing tests, less lines in parsing out namespace declarations in order to expand them to uris and then map those to the random prefixes... blah.
    If you are concerned about dropping uri's then THAT should be the exception to the rule, then you write tests to lookup the namespace-uri.
    I suppose sun's xml stack is kind of a lesson to teach us developers to handle xml the orthodox way, using random prefixes, thanks for that.
    Classic: look at the amount of code it takes to parse xml in java. Then add on complexity (weirdness) in terms of random prefixes .... then compare that to .net/python/ruby/php/perl/c/c++ ... sun is all alone on this one and about 10x more verbose.
    At least honor the xml prefixes declared and developers round the world will appreciate the boost in productivity.
    Word!
    -jhn

  • Org.w3c.dom.Document to/from String ?

    I need to be able to parse (once) an XML string, and then pass the parsed object around to/from different objects.
    I tried to use org.w3c.dom.Document,
    generated by javax.xml.parsers.DocumentBuilder .
    HOWEVER - I don't see any way to parse an xml String!
    The doc-builder parse() method accepts either a file/url, or an InputStream.
    When I tried to use the StringBufferInputStream I found-out it is deprecated, and that I should use StringReader.
    BUT I cannot parse using StringReader ! (the method parse() doesn't work with it)
    Is there anyway to parse a String to create a Document ?
    ========================================================
    And that's only HALF my problem:
    I did parse an input file into a Document, to experiment a little.
    I then wanted to convert it to an XML String - but couldn't.
    How do I get the xml String from a Document ?
    I cannot: How do I save it to a file ?
    I must say that under .NET both tasks are TRIVIAL:
    XmlDocument.LoadXML()
    XmlDocument.OuterXml
    Thanks
    Meir

    The doc-builder parse() method accepts either a file/url, or an InputStream.This isn't true. Look it up again. There are overridden versions of parse() that use File, InputSource, InputStream, or String. The String one isn't what you want, because the String it takes is a URL pointing to the XML and not the XML itself. The File one doesn't work for you because you don't have a file, and you've already said why the InputStream one doesn't work for you.
    So that leaves the version that takes an InputSource. So, what is an InputSource anyway? You could look it up just by clicking on the link in the API docs...

  • Namespace Prefix with DOM

    Hey everyone,
    I have a document and I would like to add a prefix to an existing tag so basically I want
    <Record>
    to be
    <rec:Record>
    the code I am trying is
    private static void addRecPrefix(Document inDoc) throws XPathExpressionException
            if (inDoc == null)
                return;
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//Record");
            Object evalResult = expr.evaluate(inDoc, XPathConstants.NODESET);
            if (evalResult == null)return;
            NodeList record Tags = (NodeList)evalResult;
            for(int i=0; i<recordTags.getLength() ;i++)
                Node currentNode = recordTags.item(i);
                currentNode.setPrefix("rec");
                System.out.println(currentNode.getPrefix());
        }rec is a namespace created in the parent node and it is part of the document, however, when I run the code I get a NAMESPACE_ERR.
    How am I supposed to do this?
    UPDATE!!!!!!!!
    got it to work using this inside the loop
    Node currentNode = cdfTags.item(i);
    inDoc.renameNode(currentNode,"","rec:Record");
    is this the best way?
    Edited by: partyk1d24 on Jul 9, 2008 8:22 AM

    Hi Rajiv,
    Pl. go through this blog:
    /people/stefan.grube/blog/2007/02/02/remove-namespace-prefix-or-change-xml-encoding-with-the-xmlanonymizerbean
    Also if you want to use this at sender side, SOAP adapter will not support. Please check stefans reply in this thread:
    XMLAnonymizerBean doesnt work
    Regards,
    ---Satish

  • Dom Document to String

    Hi!
    I am looking for a method to create a String from a Dom Document. I have been searching on the internet but all the methods I have found so far contain some deprecated classes :( . Can anyone help me please?
    Thanks in advanced

    georgemc wrote:
    When I saw [this cartoon|http://xkcd.com/763/], I instantly thought of some of the dancing about you have to do with JAXP sometimes
    :-)

  • Org.w3c.dom.Document  to  String?

    Hi.
    How do I create a String (or a file) from a org.w3c.dom.Document object?
    thanks in advance
    [jarimba]

    I just randomly picked one of the hundreds of posts asking this same question:
    http://forum.java.sun.com/thread.jsp?forum=34&thread=301336

  • How to create org.w3c.dom.Document.

    Hi,
    I have my complete xml file in a String variable like
    String strXml = " <?xml version="1.0"?>
    <report>
    <name>testReport & report
    <duration>Jan to Feb</duration>
    <reportId>1</reportId>
    </report> "
    I need to create a "org.w3c.dom.Document" object from the above "strXml" variable.
    Thanks in advance for your answers,
    Sha S.

    this is the code snippet that would answer u r question
    import org.apache.xml.serialize.XMLSerializer;
    import org.w3c.dom.Attr;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Text;
    import org.w3c.dom.ProcessingInstruction;
    import org.xml.sax.InputSource;
    import com.exel.businessobjects.*;
    StringReader srxml = new StringReader(xmlcontent);
    Reader rxml     = (Reader)srxml;
    InputSource isxml = new InputSource(rxml);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(isxml);

  • How to create a Document object from a string.

    If I use the following code, the input String cannot contain "\n", otherwise, it generates errors.
    in the following code, inputXMLString is a String object that has xml content.
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(inputXMLString)));          How can I create a Document object from a string with "\n" in the string?
    Thanks.

    If I use the following code, the input String cannot
    contain "\n", otherwise, it generates errors.That's going to be a huge surprise to thousands of people who process XML containing newline characters every day without errors.
    Perhaps your newline characters are in the middle of element names, or something else that causes your XML to be not well-formed. I'm just guessing here, though, because you didn't say what errors you were getting.

  • Create a DOM Document with DTD

    When creating a new DOM Document, how to specify it's DTD?
    This ...
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document d = db.newDocument();
    Element e = d.createElement("lolcats");
    d.appenChild(e);... outputs ...
    <?xml version="1.0" encoding="UTF-8"?>
    <lolcats/>.. but i want ...
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE lolcats SYSTEM "lolcats.dtd">
    <lolcats/>Plx help
    Edited by: CapM on Feb 7, 2008 11:10 AM

    Update: a workaround for my problem is to set the DTD during serialization:
    OutputFormat of = new OutputFormat(d);
    of.setDoctype(null, "lolcats.dtd");Yet not what i'm looking for.

  • How to append an xml string as a child node to a dom document

    Hi
    I have an xml represented as a String. Now I want to add this xml string as a child node to another DOM Document.
    Do I have to first parse the xml String into a xml Document and then add the nodes to the existing Document. Is there a simpler way to do this. Any input is appreciated.
    Many thanks in advance.

    radsat wrote:
    Hi
    I have an xml represented as a String. Now I want to add this xml string as a child node to another DOM Document.
    Do I have to first parse the xml String into a xml Document and then add the nodes to the existing Document. yes, this is what you need to do.
    Is there a simpler way to do this. Any input is appreciated.no, there really isn't, sorry.

  • How to put org.w3c.dom.Document into String

    Hello!
    I have some sample xml file with xml schema (xsd file). After parsing this sample file into org.w3c.dom.Document I would like to put it into the string and print out for test purpose only. But I don't know how to do it.
    I would like to print something like
    <somebody>
    <first>First somenthing</first>
    <second>Second something</second>
    </somebody>
    Thanks for your advices.

    The function you have specified works fine and gives back the string from DOM object.But it ruins all the formatting associated with the XML (like new line character)
    Is there any way to overcome it?
    Currently if i have a xml as
    <student>
    <firstname>
    </firstname>
    </student>
    The output of the code u have sent strips off the new line characters and returns everything in a single line as below:
    <student> <firstname> </firstname> </student>
    Thanks for any help associated with above problem.

  • String to org.w3c.dom.Document conversion problem

    Hi all
    I am using JDK 5 and am having scenario to convert String to w3c.Document object , by the way of finding solution i got a code snippet in sun forum
    final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><DATA>sample</DATA>"; // xml to convert
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document document = builder.parse(new InputSource(new StringReader(xml)));
    System.out.println(document);
    But when i try to execute this code , am gettng output as null. , i dont know why this code behaving like that , can you please help me to solve the problem?
    Thanks
    Prabu.P

    imprabu wrote:
    I am using JDK 5 and am having scenario to convert String to w3c.Document object, by the way of finding solution i got a code snippet in sun forum
    System.out.println(document);
    Hi Prabu,
    This is not the way you can print a DOM Document. You can do it using Transformer:
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><DATA>sample</DATA>"; // xml to convert
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    //Wong Way of printing a DOM Document. this equal to Object.toString(), Which is of no use for you.
    System.out.println(document); // It will just print: [#document: null]
    // Using Transformer to print DOM Document
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(document);
    Result output = new StreamResult(System.out);
    transformer.transform(source, output); // This will print: <?xml version="1.0" encoding="UTF-8"?><DATA>sample</DATA>*Cheers,
    typurohit* (Tejas Purohit)

  • How can I convert a String to a DOM document?

    I want to convert the text of a TextArea to a DOM Document - how can I do this?
    Thanks a million,
    Daniel.

    something.parse(new StringReader(theTextArea.getText()));

  • DOM - create new document from part of other document

    Hi.
    How to create a document from part (sub tree) of the other document?
    document 1.
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <meta1>blaa blaa</meta1>
    <meta2>blaa2 blaa2</meta2>
    <data>
    <info>
    <name></name>
    </info>
    </data>
    </root>
    document 2 shuold be:
    <?xml version="1.0" encoding="UTF-8"?>
    <data>
    <info>
    <name></name>
    </info>
    </data>
    Or can I just remove root and meta elements?

    Something like this:DocumentBuilder db = // create a DocumentBuilder;
    Document newDoc = db.newDocument();
    Node newRoot = // the Element you want to be the root of the new document;
    Node root = newDoc.adoptNode(newRoot);
    newDoc.appendChild(root);

Maybe you are looking for

  • Logical level for logical fact table sources

    it is clear that for fact aggregates, we should use the Content tab of the Logical Table Source dialog to assign the correct logical level to each dimension. question is : is it mandatory to assign even for non-aggregates fact tables the logical leve

  • How do I get Bridge and Lightroom to respect - read - metadata changes?

    I cull my wedding images in Bridge (CS 6) and then export them into LR 4.  I typically rate images as I'm culling, but when I import those images into LR, the ratings don't carry over.  Likewise, when I label images in LR and export them, Bridge show

  • Forum site for Oracle on Windows2000 or NT

    Is there any forum site for Oracle on Win2000 or NT? Thanks.

  • Acrobat 7 PDF Printer

    For those trying to get the PDF Printer device from Adobe Acrobat or CS2 to work in 10.5.x, here's a short guide to getting it going despite Adobe's claims that it can't be fixed. First, make sure that you've got the latest version of Acrobat 7 insta

  • Error 801 when executing external command

    Dear all, One of our PRODcution system (PI) system getting lot of  below error in SM21. 00:07:17 sappip_PIP_03 BTC  038 400 BASIS                LC  2 Error 801 when executing external command on (xpgid=0,convid=.) 00:07:17 sappip_PIP_03 BTC  038 400