Kurtiss
Kurtiss

Reputation: 525

A simple example of TXT to XML with Java

I have a simple text file displaying data within a list like format (input.txt).

Example One
Example Two
Example Three
...

What I want to do is use Java to convert this text file into an XML file (output.xml) stating that for each list entry, put it in a tag (such as <tag>Example One</tag> for example). I've researched into this, however the results I get are either irrelevant to what I am doing, over-complicates this simple example, or just doesn't provide enough explanation on what I need to do or how the provided solution works.

Can someone help me with what I am trying to accomplish?

Many thanks.

Upvotes: 0

Views: 24002

Answers (2)

Kurtiss
Kurtiss

Reputation: 525

There, reads text file (data.txt) and makes it into an XML file (data.xml):

import java.io.*;

import org.xml.sax.*;
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;

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

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

    public void openXml() throws ParserConfigurationException, TransformerConfigurationException, SAXException {

        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        th = tf.newTransformerHandler();

        // pretty XML output
        Transformer serializer = th.getTransformer();
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");

        th.setResult(out);
        th.startDocument();
        th.startElement(null, null, "inserts", null);
    }

    public void process(String s) throws SAXException {
        th.startElement(null, null, "option", null);
        th.characters(s.toCharArray(), 0, s.length());
        th.endElement(null, null, "option");
    }

    public void closeXml() throws SAXException {
        th.endElement(null, null, "inserts");
        th.endDocument();
    }
}

From this:

Example One
Example Two
Example Three

To this:

<?xml version="1.0" encoding="UTF-8"?>
<inserts>
    <option>Example One</option>
    <option>Example Two</option>
    <option>Example Three</option>
</inserts>

Credit goes to the author of this post. Why can't examples be this simple?

Upvotes: 4

Sanjeev
Sanjeev

Reputation: 9946

For this simple thing read your file line by line and apply transformation to the line and write to output.xml, something like this:

Open File for reading
Open File for writing.

Loop through Input file {
   String str = <read a line from file>;
   str= str.replaceAll("(.*)","<tag>$1</tag>");
   Write this string to target file.
}

Flush output file.
Close Output file.
Close Input File.

Hope this helps you in right direction.

Upvotes: 2

Related Questions