0

I have a string object "hello world" I need to create an xml file from this string with hello world as text content. I tried the following code snippet

String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"></soap:Envelope>";

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   

    DocumentBuilder builder;   
    try  
    {   
        builder = factory.newDocumentBuilder();   

        // Use String reader   
        Document document = builder.parse( new InputSource(   
                new StringReader( xmlString) ) );   

        TransformerFactory tranFactory = TransformerFactory.newInstance();   
        Transformer aTransformer = tranFactory.newTransformer();   
        Source src = new DOMSource( document );   
        Result dest = new StreamResult( new File("D:\\myXML.xml" ) );   
        aTransformer.transform( src, dest );  

    } catch (Exception e)   
    {   
        // TODO Auto-generated catch block   
        e.printStackTrace();   
    }   

this code works fine. but when i replace the string with "Hello world" its not working. Can any one help me out in this ? Thanks

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
user1002448
  • 425
  • 8
  • 22
  • How is it not working? Do you get an error? Wrong output? – Keppil Jul 13 '12 at 06:58
  • [Fatal Error] :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException: Content is not allowed in prolog. at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at com.metlife.ins.portal.idi.enrollment.gsi.vo.IDIEnrollmentBO.createXMLResponse(IDIEnrollmentBO.java:74) at com.metlife.ins.portal.idi.enrollment.gsi.vo.IDIEnrollmentBO.main(IDIEnrollmentBO.java:107) i am getting this error when i change to Hello World or some other normal string – user1002448 Jul 13 '12 at 06:59
  • Try passing the string in bytes by using getBytes() of String class.. Hopefully that will work.. – Amit Jul 13 '12 at 07:01
  • Which string are you replacing with "hello world"? – Eugenio Cuevas Jul 13 '12 at 07:02
  • What will be your XML root node ? You may place `Hello World` as text content of some node. – Alex Jul 13 '12 at 07:03
  • http://stackoverflow.com/questions/729621/convert-string-xml-fragment-to-document-node-in-java?rq=1 – Amit Jul 13 '12 at 07:03
  • i am initialising the xmlString with hello world instead of the existing formatted xml string – user1002448 Jul 13 '12 at 07:04
  • That wont work. "hello world" would need to be placed inside a xml node. Like `hello world` or something. – Scorpio Jul 13 '12 at 07:06

4 Answers4

2

You cannot turn the string "hello world" into XML, as it is not a valid xml document. It has no declaration, and no tags.

The code above will not turn text into xml objects, it will only take a string which is already valid xml and write it out to file.

To be honest, if you just want to write it to a file, the xml stuff is all unnecessary.

If you want some kind of "hello world" xml file, you'll need to add the declaration and some tags yourself.

Rob Trickey
  • 1,301
  • 11
  • 13
1

This error is because you are trying to parse xmlString as a valid XML string, which it is not. For example, your code will run fine with the following xmlString:

String xmlString = "<hi>Hello World</hi>";
Keppil
  • 45,603
  • 8
  • 97
  • 119
0

If you have String newNode = "<node>Hello World</node>";

You can use

 Element node =  DocumentBuilderFactory
    .newInstance()
    .newDocumentBuilder()
    .parse(new ByteArrayInputStream(newNode.getBytes()))
    .getDocumentElement();
Amit
  • 13,134
  • 17
  • 77
  • 148
0

The simplest solution can be here is: If it's a valid string(correct as per XML norms) just write it into a new file using FileWriter and give it .xml extension. Anyway it will not convert if it's not a valid XML string

Pranav Kevadiya
  • 519
  • 1
  • 6
  • 17