Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example2;
- 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.*;
- import java.io.*;
- import java.util.StringTokenizer;
- public class queries {
- BufferedReader in;
- StreamResult out;
- TransformerHandler th;
- AttributesImpl atts;
- public static void main(String args[]) {
- new queries().doit();
- }
- public void doit() {
- try {
- in = new BufferedReader(new FileReader("C:/Users/Teo-Maria/Desktop/MED.QRY"));
- out = new StreamResult("C:/Users/Teo-Maria/Desktop/data2.xml");
- initXML();
- String str;
- while ((str = in.readLine()) != null) {
- System.out.println(str);
- process(str);
- }
- in.close();
- closeXML();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void initXML() throws ParserConfigurationException,
- TransformerConfigurationException, SAXException {
- SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
- .newInstance();
- th = tf.newTransformerHandler();
- Transformer serializer = th.getTransformer();
- serializer.setOutputProperty(OutputKeys.INDENT, "yes");
- th.setResult(out);
- th.startDocument();
- atts = new AttributesImpl();
- th.startElement("", "", "add", atts);
- }
- public void process(String s) throws SAXException {
- //String[] elements = s.split("\\n|\\\\.(?!\\\\d)|(?<!\\\\d)\\\\.");
- atts.clear();
- // System.out.println(elements[0]);
- StringTokenizer aString = new StringTokenizer(s);
- // get first token from line
- String str1 = aString.nextToken();
- if(str1.equals(".I")) {
- th.startElement("", "", "id", atts);
- th.characters(s.toCharArray(), 0, s.length());
- th.endElement("", "", "id");
- }else {
- th.startElement("", "", "content", atts);
- th.characters(s.toCharArray(), 0,s.length());
- th.endElement("", "", "content");
- }
- }
- public void closeXML() throws SAXException {
- th.endElement("", "", "add");
- th.endDocument();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement