📄 myhandler.java
字号:
package tw.com.javaworld.CH17;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class MyHandler extends DefaultHandler {
private StringBuffer textBuffer;
private HashMap hm_TagData;
private String tagName;
public MyHandler(HashMap hm_TagData) {
this.hm_TagData = hm_TagData;
}
//当SAX遇到xml字符时,会自动调用的方法
public void characters(char[] ch, int start, int len) {
String s = new String(ch, start, len);
if (textBuffer == null) {
textBuffer = new StringBuffer(s);
} else {
textBuffer.append(s);
}
}
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
// 判断重复的属性名称
int count = 1;
echoText();
if (atts != null) {
for (int i = 0; i < atts.getLength(); i++) {
// 属性名称
String aName = atts.getLocalName(i);
if ("".equals(aName))
aName = atts.getQName(i);
while (hm_TagData.get(aName) != null) {
aName = aName + count;
count++;
}
hm_TagData.put(aName, atts.getValue(i));
}
}
}
public void endElement(String namespaceURI, String localName, String qName) {
String eName = localName;
// 标签名称
if ("".equals(eName))
eName = qName;
// 当namespaceAware为false时
int count = 1;
while (hm_TagData.get(eName) != null) {
eName = eName + count;
count++;
}
hm_TagData.put(eName, textBuffer);
echoText();
}
private void echoText() {
if (textBuffer == null)
return;
String s = "" + textBuffer;
textBuffer = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -