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文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE raml SYSTEM 'raml20.dtd'>
<raml version="2.0" xmlns="raml20.xsd">
<cmData type="actual">
<header>
<log dateTime="1889-10-10T02:21:13" action="created" appInfo="SctualExr">InternalValues are used</log>
</header>
<managedObject class="BFC" version="S16_2" distName="PLMN-PLMN/BSC-209943" id="103SA51">
<list name="bscOptions">
<p>0</p>
<p>1</p>
<p>2</p>
<p>3</p>
<p>5</p>
<p>6</p>
</list>
<p name="dcsMacrocellThreshold">2</p>
<p name="dcsMicrocellThreshold">3</p>
<p name="name">BSC144</p>
<p name="neSwRelease">S16_2</p>
<p name="AIfAf1WfqWeight">5000</p>
<p name="AIfAf2WfqWeight">5000</p>
<p name="AIfAf3WfqWeight">5000</p>
<p name="amrSSGradesEnabl">0</p>
<p name="ansDvironment">0</p>
<p name="batDSckupTimer">30</p>
<p name="bcsuLoadThr">150</p>
<p name="bhTimFSDFurStartReact">4</p>
<p name="bhTimerDurStopReact">10</p>
<p name="pcuMaxNoDLtbfInCH">9</p>
<p name="pcuMaxNoULtbfInCH">7</p>
<list name="pieToSubscriberType">
<p>2</p>
<p>0</p>
<p>2</p>
<p>0</p>
</list>
<p name="prdHighIf">120</p>
<p name="prdNokBts">120</p>
<p name="preemptionUsageInHO">1</p>
<p name="psPacketDropPeriod">25</p>
<p name="vpBe">0</p>
<p name="vpEf">6</p>
<p name="wcdmaRanSaiPenalty">127</p>
</managedObject>
</cmData>
</raml>
没有创建实体类,适合大文件解析