cancel
Showing results for 
Search instead for 
Did you mean: 

convert string to xml

Former Member
0 Kudos

I have string

xml_file =" <content is as below>";

<?xml version='1.0' encoding='UTF-8' standalone='no'?><Motor>

<MotorElement><platno>jacket</platno>

<type>blue</type>

</MotorElement>

<MotorElement><platno>skirt</platno>

<type>red</type>

</MotorElement>

</Motor>

how do i change to xxx.xml

this is the sample to change a file to xml ...but i want a string to xml


Java|http://www.rgagnon/javahowto.htm
PowerBuilder|http://www.rgagnon/pbhowto.htm
Javascript|http://www.rgagnon/jshowto.htm
VBScript|http://www.rgagnon/vbshowto.htm



<?xml version="1.0" encoding="ISO-8859-1"?>
<HOWTOS>
    <TOPIC>
        <TITLE>Java</TITLE>
        <URL><a href="http://www.rgagnon/javahowto.htm" TARGET="test_blank">http://www.rgagnon/javahowto.htm</a></URL>
    </TOPIC>
    <TOPIC>
        <TITLE>PowerBuilder</TITLE>
        <URL><a href="http://www.rgagnon/pbhowto.htm" TARGET="test_blank">http://www.rgagnon/pbhowto.htm</a></URL>
    </TOPIC>
    <TOPIC>
        <TITLE>Javascript</TITLE>
        <URL><a href="http://www.rgagnon/jshowto.htm" TARGET="test_blank">http://www.rgagnon/jshowto.htm</a></URL>
    </TOPIC>
    <TOPIC>
        <TITLE>VBScript</TITLE>
        <URL><a href="http://www.rgagnon/vbshowto.htm" TARGET="test_blank">http://www.rgagnon/vbshowto.htm</a></URL>
    </TOPIC>
</HOWTOS>

import java.io.*;

// SAX classes.
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*;

public class ToXML  {

  BufferedReader in;
  StreamResult out;

  TransformerHandler th;
  AttributesImpl atts;

  public static void main (String args[]) {
      new ToXML().doit();
  }

  public void doit () {
    try{
      in = new BufferedReader(new FileReader("data.txt"));
      out = new StreamResult("data.xml");
      initXML();
      String str;
      while ((str = in.readLine()) != null) {
         process(str);
      }
      in.close();
      closeXML();
    }
    catch (Exception e) { e.printStackTrace(); }
  }


  public void initXML() throws ParserConfigurationException,
      TransformerConfigurationException, SAXException {
    // JAXP + SAX
    SAXTransformerFactory tf = 
       (SAXTransformerFactory) SAXTransformerFactory.newInstance();

    th = tf.newTransformerHandler();
    Transformer serializer = th.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
    // pretty XML output
    serializer.setOutputProperty
        ("{http://xml.apache.org/xslt}indent-amount", "4");
    serializer.setOutputProperty(OutputKeys.INDENT,"yes");
    th.setResult(out);
    th.startDocument();
    atts = new AttributesImpl();
    th.startElement("","","HOWTOS",atts);
  }

  public void process (String s) throws SAXException {
    String [] elements = s.split("\|");
    atts.clear();
    th.startElement("","","TOPIC",atts);

    th.startElement("","","TITLE",atts);
    th.characters(elements[0].toCharArray(),0,elements[0].length());
    th.endElement("","","TITLE");

    th.startElement("","","URL",atts);
    th.characters(elements[1].toCharArray(),0,elements[1].length());
    th.endElement("","","URL");

    th.endElement("","","TOPIC");
  }

  public void closeXML() throws SAXException {
    th.endElement("","","HOWTOS");
    th.endDocument();  }
}

Message was edited by:

yzme yzme

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

If you have a xml string, just write it to a file using FileWriter. The code might look something like:

FileWriter writer = new FileWriter("fileName.xml",false);
writer.write(xml_string);

Regards,

Satyajit.

Former Member
0 Kudos

String xmlfile="<?xml version='1.0' encoding='UTF-8' standalone='no'?><Motor><MotorElement><platno>jacket</platno><type>blue</type></MotorElement></Motor>";
		FileWriter writer = new FileWriter("d:\Motor.xml",false);
		writer.write(xmlfile);

after i create the motor.xml ...it is 0 kb and no content....pls advice

Former Member
0 Kudos

Did you flush the stream and close it..

writer.flush()
writer.close()

Regards

-Bharathwaj

Answers (1)

Answers (1)

monalisa_biswal
Contributor
0 Kudos

Go through this tutorial.

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/downloaditem?rid=/library/uuid/1208c2cd-0401-0010-4ab6-f4736074acc6">https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/downloaditem?rid=/library/uuid/1208c2cd-0401-0010-4ab6-f4736074acc6</a>

It converts string to .XLS file.To change it to .xml file pass WDWebResourceType.XML mime type to getCachedWebResource method.