Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package example2;
  2. import org.xml.sax.*;
  3. import org.xml.sax.helpers.*;
  4. import javax.xml.parsers.*;
  5. import javax.xml.transform.*;
  6. import javax.xml.transform.stream.*;
  7. import javax.xml.transform.sax.*;
  8. import java.io.*;
  9. import java.util.StringTokenizer;
  10.  
  11.  
  12.  
  13. public class queries {
  14.  
  15.     BufferedReader in;
  16.     StreamResult out;
  17.     TransformerHandler th;
  18.     AttributesImpl atts;
  19.  
  20.  
  21.     public static void main(String args[]) {
  22.         new queries().doit();
  23.     }
  24.  
  25.     public void doit() {
  26.         try {
  27.             in = new BufferedReader(new FileReader("C:/Users/Teo-Maria/Desktop/MED.QRY"));
  28.             out = new StreamResult("C:/Users/Teo-Maria/Desktop/data2.xml");
  29.             initXML();
  30.             String str;
  31.             while ((str = in.readLine()) != null) {
  32.                 System.out.println(str);
  33.                 process(str);
  34.             }
  35.             in.close();
  36.             closeXML();
  37.         } catch (Exception e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.  
  42.     public void initXML() throws ParserConfigurationException,
  43.             TransformerConfigurationException, SAXException {
  44.         SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
  45.                 .newInstance();
  46.  
  47.         th = tf.newTransformerHandler();
  48.         Transformer serializer = th.getTransformer();
  49.         serializer.setOutputProperty(OutputKeys.INDENT, "yes");
  50.         th.setResult(out);
  51.         th.startDocument();
  52.         atts = new AttributesImpl();
  53.         th.startElement("", "", "add", atts);
  54.     }
  55.  
  56.     public void process(String s) throws SAXException {
  57.         //String[] elements = s.split("\\n|\\\\.(?!\\\\d)|(?<!\\\\d)\\\\.");
  58.         atts.clear();
  59.     //  System.out.println(elements[0]);
  60.         StringTokenizer aString = new StringTokenizer(s);
  61.         // get first token from line
  62.         String str1 = aString.nextToken();
  63.        
  64.         if(str1.equals(".I")) {
  65.         th.startElement("", "", "id", atts);
  66.         th.characters(s.toCharArray(), 0, s.length());
  67.         th.endElement("", "", "id");
  68.        
  69.         }else {
  70.             th.startElement("", "", "content", atts);
  71.             th.characters(s.toCharArray(), 0,s.length());
  72.             th.endElement("", "", "content");
  73.         }
  74.    
  75.     }
  76.    
  77.  
  78.     public void closeXML() throws SAXException {
  79.         th.endElement("", "", "add");
  80.         th.endDocument();
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement