Convert XML string into DOM

Hi all,
I have a question here regarding converting XML string into a DOM document.
How can I do this in Java?
The Document.Parse() methods do not have one that takes in String as its parameter.
I'm not sure what is the best way to do this in Java.
Any help is greatly appreciated.
Thank you in advance.
regards,
Sean

Convert XML String to Document with StringReader.
String xmlString;
DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
   Document         document = builder.parse(new InputSource(new StringReader(xmlString)));

Similar Messages

  • XML: How to convert xml string into Node/Document

    I have a string in xml form.
    <name>
    <first/>
    <second/>
    </name>
    I want to convert this string into DOM Node or Document.
    How can I do this?
    Any help or pointer?
    TIA
    Sachin

    Try this :
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new org.xml.sax.InputSource(new StringReader(strXml)));Hope this helps.

  • Special charecters handling while Converting XML string to DOM

    Hi,
    I am using the following approach for converting XML string to DOM, but due to Special characters like "&", I am getting Exceptions:
    String xmlString;
    DocumentBuilderFactory factory =
    DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xmlString)));
    Can anyone please help me out on how to handle the Special characters in the above code.

    If the XML doesn't parse, then the XML must be invalid. Show a sample of such a special character in the XML data. Please use \ tags to post the actual XML content and any other code.                                                                                                                                                                                                                                                                                                                                                                                           

  • Convert XML string into XML

    Hi All,
    Can you please let me know for any sample code in xslt/java mapping for converting XML string into XML. We use SAP Pi 7.0
    My XML string starts like this
    <?xml version="1.0" encoding="UTF-8" ?> 
    - <ns0:MT_ReceiverFileStructure <namespace>"><Output><?xml version="1.0" encoding="ISO-8859-9"?><?xml-stylesheet type="text/xsl" href="<xsl>"?><Tarih_Date Tarih="11.09.2014" Date="09/11/2014>
       Thanks,
       Pavithra

    Thanks Praveen. It worked.
    However, the xml i have is an extract from a exchange rate URL and it has the reference to a xsl in it as below
    <?xml version="1.0" encoding="ISO-8859-9"?><?xml-stylesheet type="text/xsl" href="<ABC.xsl>"?>.
    So there is an error in sxmb_moni. Is it possible to remove this.

  • Convert XML string into an abap format date and time

    Hi,
    Does anyone know of a method or a function module in ABAP which converts XML string into an abap format date and time.
    Here is a part of the xml that I want to convert.
    <ns2:EventDateTime>2009-07-02T10:13:45+10:00</ns2:EventDateTime>
    <ns2:EventMessageTransmissionDateTime>2009-07-02T10:13:45.987+10:00</ns2:EventMessageTransmissionDateTime>
    Currently EventDateTime and EventMessageTransmissionDateTime are type XSDDATETIME_Z and these are converted to proper dates and times. We will be changing these fields to a STRING instead of XSDDATETIME_Z. The reason for this is to make the field more versatile. The customer would be receiving dates with Zulu (2009-09-23T12:00:00Z), with offsets (2009-09-23T12:00:00+10:00/-10:00) and just local timestamp (2009-09-23T12:00:00). With this, we need to make the date fields as string
    to be able to accept the various date formats (esp. the local timestamp).
    I am looking for a standard function module or method that will convert the xml string to a proper date and time abap format.
    Would appreciate anyone's help!
    Thanks.
    Edited by: eunice ocson on Jul 13, 2009 1:49 AM
    Edited by: eunice ocson on Jul 13, 2009 1:50 AM
    Edited by: eunice ocson on Jul 13, 2009 1:51 AM
    Edited by: eunice ocson on Jul 13, 2009 1:51 AM

    Hi Eunice
    Use the FM 'SMUM_XML_PARSE'
    for more info
    [Convert XML string to ABAP|XML String to ABAP or GUI]
    hope it helps you.
    Thanks!!

  • Convert xml string into xml file?

    Hi,
    I got a method that returns an JDom Element object.
    Then I use the JDOM Element toString method() to change it into xml string.
    Now, I need to convert it into actual xml file.
    How to I do it?
    Thanks.

    Hi,
    If you got an xml string, just write it to an ordinary file. Look at FileWriter.
    /Kaj

  • Converting XML string to ABAP types

    Hello,
    I am having a requirement where i wanted to parse incoming XML string and i checked few of examples of CALL TRANSFORMATION id.
    but whatever examples i checked those were simple transformation with no hierarchy.
    My source structure
    <record>
    <firstname>XYZ</firstname>
    <position>Testposition</postion>
    </record>
    and i used
    CALL transformation id
    source xml lv_xml
    result firstname = lv_firstname position = lv_position.
    but lv_firstname and lv_position were blank. any idea whether it can be acheived through such method?
    thanks in advance.
    regards
    rajeev

    Hello Rajeev,
    Try using the following simple transformation
    <?sap.transform simple?>
    <tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
      <tt:root name="record"/>
      <tt:template name="record">
        <record>
          <firstname>
            <tt:value ref="record.firstname"/>
          </firstname>
          <position>
            <tt:value ref="record.position"/>
          </position>
        </record>
      </tt:template>
    </tt:transform>
    In your code you could define a structure like the following:
    TYPES: BEGIN OF record,
                  firstname TYPE char50,
                  position  TYPE char50,
                END OF record.
    And now you could use the CALL TRANSFORMATION in your code to convert the xml string into the structure and visa-versa.
    CALL TRANSFORMATION zcl_test_rec
          SOURCE XML lv_string
          RESULT record = ls_test.
    Where lv_string holds the xml as a string and ls_test is defined as a structure of type record (defined above).
    Hope this helps.
    Thank you,
    Ramneek

  • Converting XML String to MessageElement array

    Hi,
    I am trying to call a .NET web service from my Java client. I used WSDL2Java to generate the java classes for calling the .NET web service. The generated classes to call the service expects an array of org.apache.axis.message.MessageElement objects. I have a string representing an XML document which looks like this:
    String xmlString = "<Results><Adjustments><Adjustment><RebuildAdjustmentID>16</RebuildAdjustmentID><IsBasicAdjustment>true</IsBasicAdjustment><AdjustmentType>stone/AdjustmentType><Title>External walls</Title></Adjustment></Adjustments></Results>"
    I have tried converting the string into an array of MessageElement objects by the following way:
    MessageElement[] m = new MessageElement[1];
    Document XMLDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(result2.toString())));
    m[0] = XMLDoc.getDocumentElement();
    However I keep getting the following message returned from the service:
    "Object reference not set to an instance of an object"
    I have tried a handful of ways but keep getting this same error. I have searched the web for hours looking for a solution to this problem without success so any help/ideas much appreciated,
    Thanks.
    Paul

    Any updates on this?
    I am facing a similar problem.

  • A class to format an XML string into indented xml code

    I am looking for a class or a piece of code to format an XML string into indented xml code
    for example: an XML string as follows
    <servlet><servlet-name>Login</servlet-name>servlet-class>ucs.merch.client.system.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>Login</servlet-name>
    to format into :
    <servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>ucs.merch.client.system.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
    </servlet-mapping>
    e-mail : [email protected]

    Xerces has a class called OutputFormat
    If you have your XML document in memory, you can format it using the method setIndenting(true) on the OutputFormat class. The following is an example:
    assuming xmlDoc is our document and fileName is the name of the file we wish to write to:
    OutputFormat format = new OutputFormat(xmlDoc);
    // setup output file name
    PrintWriter printwriter = new PrintWriter(new FileWriter(fileName, false));
    // construct an XMLSerializer for writing the document
    XMLSerializer serializer = new XMLSerializer( printwriter, format );
    // Ensure output is indented correctly...
    format.setIndenting(true);
    // set serializer as a DOM Serializer
    serializer.asDOMSerializer();
    // serialize the document
    serializer.serialize(xmlDoc);
    hope this helps!
    Rob.

  • How to get an xml string into a Document w/o escaping mark-up characters?

    Hi,
    I am using one of the latest xerces using Java. I am pretty sure I am using xerces-2.
    I have an existing Document and I am trying to add more content to it. The new content itself is xml string. I am trying to insert this xml string into the document using document.createTextNode. I am able to insert, but somewhere it is escaping the mark-up characters (<,>,etc). When I convert the document into String, I can see, for example, <userData> instead of <userData>.
    There is an alternative option to accomplish this by creating a new document with this xml string, get the root element, import this element into my document. Execution time for this procedure is very high - means, this is very bad in terms of time-wise performance.
    Can any help on how to accomplish this (bringing an xml string into a document without escaping mark-up characters) in time-efficient way.

    So you want to treat the contents of the string as XML rather than as text? Then you have to parse it.
    Or if your reason for asking is just that you don't like the look of escaped text, then use a CDATA section to contain the text.

  • Does anyone can help me to import XML string into a ORACLE database

    Hi guys!
    i have some problem that my friend wants me to convert XML string to Oracle relational database which means.
    let's say, there is a table that a row has XML string.
    so table has XML string with varchar type.
    what my friend want me to do is, the string needs to be solved as a relational database.
    like
    A table has 'ID', 'data', 'result' rows
    and Data column has XML string like <?xml version...............>
    then wants to convert that data column's XML data into TABLE B with structure normally XML has.
    i'm so confused and have no idea at all.. also i couldn't find any solution related to my stuff..
    if someone knows how to solve the problem, just let us know
    thx

    The database is 10.2.0
    What sort of example do u want me to give you..
    i thought i give all information on the post..
    i will explain it
    let's say there are 3 columns ID,Name,Code
    Id = normal integer
    Name= varchar
    Code= varchar
    and i need to conver that XML in the Code column into relational databases
    the code column has normal XML code which is starting from <?xml version.... and so on
    so does it explain?
    please help me!

  • Convert XML file into PDF file

    <b>XML to PDF</b>i want to Convert XML file into PDF file pl. any one can suggests API's

    Hi,
    There are many ways to convert XML files to PDFs through java.
    One of the easiest way is by using iText.jar which have classes for conversion.
    The following are the required steps
    1. Create a document object for the XML file ( Using DOM or SAX parser).
    2. Parse the xml document and extract the content to write in PDF.
    3.Create a itext Document object.
    4.Get a PdfWriter instance for the PDF file.
    5.Write in the pdf the extracted text using the document object.
    Refer <a href="http://itextdocs.lowagie.com/examples/com/lowagie/examples/general/HelloWorld.java">here</a> for a simple pdf writer example.
    Refer <a href="http://java.sun.com/developer/codesamples/xml.html">here</a> for examples of XML Parsers.
    Regards,
    Uma

  • REG: WRITE XML STRING INTO XML FILE

    Hi All, I have a XML FILE on PC. I get a xml string from my server.
    My Task: I want to append the xml string coming from the server into the existing xml file at a particular Node. Given below is my code :
    String xmlString = baos.toString();
                      DocumentBuilder db4XMLString = docBuilderFactory.newDocumentBuilder();
                      InputSource inStream = new InputSource();
                      inStream.setCharacterStream(new StringReader(xmlString));
                      Document doc4XMLString = db4XMLString.parse(inStream);
                      Node root4XMLString = doc4XMLString.getElementsByTagName("epg").item(0);
                      doc.appendChild(doc.importNode(root4XMLString, true));
                      File file = new File(Constant.XMLFILEPATH);
                      Result result = new StreamResult(file.toURI().getPath());
                      Source source = new DOMSource(doc);
                      Transformer xformer = TransformerFactory.newInstance().newTransformer();
                      xformer.transform(source, result);But, wen i run my application I get following exception:
    The markup in the document following the root element must be well-formed.
    org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    Is that my XML string is not well formed. Is it so then please let me how to convert my xml string into well formed XML data.
    Please help me out.
    Thanking You.

    There are many reasons why xmlString could be not well formed. There is no magic recipe for making it well formed: just look at it and see why it is not parseable.
    Note that this line:
    String xmlString = baos.toString();can be problematic if you have non ASCII-7 characters in your XML document. baos could then contain a byte representation of these characters in one particular encoding, but, since you use the .toString() method that relies on the default encoding of your system, you can end up with a mismatch and garbage in your XML.

  • How do I convert a string into a decimal

    I've been tryin to convert a string into decimal but I can't get it to work at all. I keep getting an exception.
    I've tried all of these:
    double myDouble = Double.valueOf(string).doubleValue();
    int myint = Interger.parseInt(string);
    float myfloat = Float.parseFloat(string);
    I can't get anything to work.
    That string i'm reading in is an array containing values with decimals. ex: -45.09348
    All the above work fine if there is no decimal, but fails when it comes to one.
    How do I go about doing this?
    Thanks!

    This works for me:String s = "-45.6789";
    double d = Double.parseDouble(s);
    System.out.println(d);Mark

  • How do you convert this String into a double?

    Hi, in this code I want to convert String arg into a double. I'm trying to do this so that I can convert some string into a double in this calculator that I'm making. Here is the code:
    public class par {
    public static void main(String[] args) {
    String str = "351";
    double arg = new double(str); // should convert the string into a double
         System.out.println(arg);
         System.out.println(str);
    I keep getting errors in line 4, if any of you could help me out here I'll be thankful, i'm kinda new to java and my search for java's equivalent of alof() (its a function in C libraries that converts char[] into a double)lead me to this.
    Tony L.

    Use the Following:
    String num = "123.2";
    double d = Double.parseDouble(num);

Maybe you are looking for

  • Creating transfer order for delivery with ABAP

    Hi Experts, I'm using L_TO_CREATE_DN to create a transfer order for delivery note... Is it possible to use this function or another one, to create a transfer order for one position of the delivery note ? Regards, David

  • How to use comments in tcode v/ld ? We are using price condition PR00.

    HI All, How to use comments in tcode v/ld ? if we use text internal comments( Ex: ABCD) in VK12 for Condition PR00. Tcode V/LD pricing report as 16 and we need that text comments(EX:ABCD) in V/LD report. Suggest me on this. Thanks Naveen

  • How can I get Premier Elements to recognize my Canon Vixia  HV40 to record tape from it?

    I recently installed Adobe Premier Elements 12 on my new Lenovo U310 Ideapad computer.  I opened the program and was able to transfer a clip from a file transfered to me from a smart phone recording and start assembling a new home movie.  However, wh

  • Default value of combobox using dynamic LOV in Oracle Portal form

    We are trying to specify the deafult value of a combobox on a form that derives it options from a dynamic LOV. The option value is a number and the display is text. E.g. <Option value="1">Books <Option value="2">CDs We have tried various ways of sett

  • Maxdb has corrupt datafile

    Hi Everyone, I have my content server running Maxdb 7.3 and it ran out of space when the autoextend was tring to add a datafile.  The database is not able to start now due to the corrupt datafile.  Can you offer any assistance? Is it possible to upgr