使用SAX解析超大xml并转存成cvs


public class LearnSAX {
     

   public static void main(String[] args) throws Exception, SAXException {
     
   	SAXParserFactory factory = SAXParserFactory.newInstance();
   	SAXParser parser = factory.newSAXParser();
        
   	Date before = new Date();
   	parser.parse(new File("E://TEST.xml"), new MyHandler());
   	Date after = new Date();
   	System.out.println("it takes " + (after.getTime() - before.getTime())
   			+ "ms");
   }

}

class MyHandler extends DefaultHandler {
     

   OutputStreamWriter out = null;
   OutputStreamWriter out2 = null;
   private String tagName;
   private String id;

   public String getId() {
     
   	return id;
   }

   public void setId(String id) {
     
   	this.id = id;
   }

   public String getTagName() {
     
   	return tagName;
   }

   public void setTagName(String tagName) {
     
   	this.tagName = tagName;
   }

   @Override
   public void startDocument() throws SAXException {
     
   	try {
     

   		out = new OutputStreamWriter(new FileOutputStream("D:/xml2csv.csv"), "UTF-8");

   		FileOutputStream fileOutputStream = new FileOutputStream("D:/xml2csv2.csv");

   		out2 = new OutputStreamWriter(new FileOutputStream("D:/xml2csv2.csv" ), "UTF-8");
   		try {
     
   			out.write("mess_class,mess_version,distName"+","+"mess_id" +"\n");
   			out2.write("tar_id"+","+"tar_name" +"\n");
   		} catch (IOException e) {
     
   			e.printStackTrace();
   		}
   	} catch (UnsupportedEncodingException e) {
     
   		e.printStackTrace();
   	} catch (FileNotFoundException e) {
     
   		e.printStackTrace();
   	}
   }

   @Override
   public void endDocument() throws SAXException {
     
   	try {
     
   		out.close();
   		out2.close();
   	} catch (IOException e) {
     
   		e.printStackTrace();
   	}
   }

   @Override
   public void startElement(String uri, String localName, String qName,
   		Attributes attributes) throws SAXException {
     
   	if(qName.equals("managedObject")){
     
   		int size=attributes.getLength();
   		StringBuffer stringBuffer =new StringBuffer();
   		for(int i=0;i<size;i++){
     


   			stringBuffer.append(attributes.getValue(i)+",");
   			if(i == size-1){
     
   				this.id  = attributes.getValue(i);
   			}
   		}
   		System.out.println(stringBuffer.toString());
   		try {
     
   			out.write(stringBuffer.toString() +"\n");
   		} catch (IOException e) {
     
   			e.printStackTrace();
   		}
   	}
   	this.tagName = qName;
   }

   @Override
   public void endElement(String uri, String localName, String qName)
   		throws SAXException {
     
   	if (qName.equalsIgnoreCase("raml")) {
     
   		try {
     
   			out.write("\r\n");
   		} catch (IOException e) {
     
   			e.printStackTrace();
   		}
   	}
   }

   @Override
   public void characters(char ch[], int start, int length)
   		throws SAXException {
     
   	String s = new String(ch, start, length);
   	if (!s.trim().isEmpty() && this.tagName.equals("p"))
   		try {
     
   			out2.write(this.id+","+s +"\n");
   		} catch (IOException e) {
     
   			e.printStackTrace();
   		}
   }

}

部分xml文件




  
    
InternalValues are used

0

1

2

3

5

6

2

3

BSC144

S16_2

5000

5000

5000

0

0

30

150

4

10

9

7

2

0

2

0

120

120

1

25

0

6

127

没有创建实体类,适合大文件解析

你可能感兴趣的:(java基础,java,sax,xml)