Hi,
I was asked to convert a text file in the format
10_1 a;b<>cd<>ef
to be converted into xml as
<ID> 10_1 </ID>
<name>
<Name1> a </Name1>
<Name2> b </Name2>
</name>
<Def> cd </def>
<Ven> ef </Ven>
I am having problem in parsing the first part of the problem, splitting the ID and the name.
I am attaching the code I have written so far, and please do not post suggestions or comments as to how to do it, I have looked into a lot of examples but I am having continuous error issues.
What I have tried:
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("E.txt"));
out = new StreamResult("E.xml");
initXML();
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
closeXML();
} catch (IOException | ParserConfigurationException | TransformerConfigurationException | SAXException e) {
}
}
public void initXML() throws ParserConfigurationException,
TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
th = tf.newTransformerHandler();
Transformer serializer = th.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
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("", "", "Author", atts);
}
public void process(String s) throws SAXException {
String[] elements = s.split("<>;");
atts.clear();
th.startElement("", "", "Data", atts);
th.startElement("", "", "Bibliofile", atts);
th.characters(elements[0].toCharArray(), 0, elements[0].length());
th.endElement("", "", "Bibliofile");
th.endElement("", "", "Data");
}
public void closeXML() throws SAXException {
th.endElement("", "", "Author");
th.endDocument();
}
}