Skip to Content
author's profile photo Former Member
Former Member

how to transform a XML String to XML document

Hi,

how to transform a XML String to XML document

eg: String xmlstring="<root><main>Title</main></root>";

How can I transform this to XML Document??

Regards,

Mahesh

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Sep 20, 2006 at 10:26 AM

    Refer this tutorial:

    https://www.sdn.sap.com/irj/sdn/downloaditem?rid=/library/uuid/1208c2cd-0401-0010-4ab6-f4736074acc6

    Don't go by the title of the document, in this tutorial, they explain how to convert an XML string to XML format and thus open it using an Excel application.

    Regards,

    Subramanian V.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Sebastian,

      Thanks for the support, I am using a DOM parser and was able to convert a string to an XML Document.

      Now I have a problem that additional Newline characters get introduced in the document.

      Eg : when I get the length of ChildNodes, I get the number of childnodes + the newlines in between each child node.

      i.e if a node has 2 children I get the length as 5 (2children + 3 blank nodes).

      Please let me know how to remove these newlines ??

      Awaiting your kind responses.

      Regards,

      Mahesh

  • author's profile photo Former Member
    Former Member
    Posted on Oct 25, 2006 at 09:15 AM

    Hi Mahesh,

    Clean your xml using followin code i.e remove all empty nodes. These empty nodes may have name as null or "#text".

    try

    {

    Document doc = builder.parse(inputstrm);

    Element root = doc.getDocumentElement();

    NodeList ndLst = root.getChildNodes();

    for(int i=0;i<ndLst.getLength();i++)

    {

    Node tmp = ndLst.item(i);

    if(tmp.getNodeName().compareToIgnoreCase("") == 0 || tmp.getNodeName().compareToIgnoreCase("#text") == 0)

    {

    root.removeChild(tmp);

    }

    }

    }

    catch(Exception e)

    {}

    Hope this solves your problem

    Regards,

    Mayank

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.