📄 logparser.java
字号:
package com.jobcn.control;
import java.util.Arrays;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.Locator;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
public class LogParser extends DefaultHandler {
private String fileName;
private int[] keys;
private int len;
private StringBuilder[] buffers;
private String tempStr, currNameValue, currIDValue;
private long start, end;
public LogParser() {
super();
}
public void setDocumentLocator(Locator locator) {
}
public void startDocument() throws SAXException {
Arrays.sort(keys);
buffers = new StringBuilder[len];
for(int i=0;i<len;i++) {
buffers[i] = new StringBuilder();
}
System.out.println("*******开始解析文档*******");
start = System.currentTimeMillis();
}
public void endDocument() throws SAXException {
end = System.currentTimeMillis();
System.out.println("cost times: "+(end-start));
System.out.println("*******文档解析结束*******");
/*for(int i=0;i<len;i++) {
System.out.println(i+"**************\r\n"+buffers[i].toString());
}*/
}
public void startPrefixMapping(String prefix, String uri) {
System.out.println(" 前缀映射: " + prefix + " 开始!" + " 它的URI是:" + uri);
}
public void endPrefixMapping(String prefix) {
System.out.println(" 前缀映射: " + prefix + " 结束!");
}
public void processingInstruction(String target, String instruction)
throws SAXException {
}
public void ignorableWhitespace(char[] chars, int start, int length)
throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) {
//System.out.println("*******开始解析元素*******");
//System.out.println("元素名" + qName);
currNameValue = atts.getValue(0);
currIDValue = atts.getValue(1);
/*for (int i = 0; i < atts.getLength(); i++) {
System.out.println("元素名:" + atts.getQName(i) + ";属性值:"
+ atts.getValue(i));
}*/
}
public void endElement(String namespaceURI, String localName,
String fullName) throws SAXException {
//System.out.println("******元素解析结束********");
}
public void characters(char[] chars, int start, int length)
throws SAXException {
// 将元素内容累加到StringBuffer中
if (currNameValue != null && currNameValue.equals(fileName)) {
tempStr = new String(chars, start, length);
int index = Arrays.binarySearch(keys, Integer.parseInt(currIDValue));
buffers[index].append(tempStr);
}
}
public static void main(String args[]) {
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
LogParser test = new LogParser();
sp.parse(new InputSource("D:/project/ScheduleNew/log/message.xml"), test);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setKeys(Object[] keys) {
len = keys.length;
this.keys = new int[len];
for (int i=0;i<len;i++) {
this.keys[i] = (Integer)keys[i];
}
}
public StringBuilder[] getBuffers() {
return buffers;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -