您可以使用以下代码的修改版本来捕获实体的开始和结束。执行需要几秒钟,因为解析器必须获取所有 HTML latin1 实体的声明。当您获得一个不以 开头的实体时%
,您可以替换acc
缓冲区中插入的字符。注意预定义的实体,例如&
.
您还可以自动对作业使用 Sax 过滤器。参照。回答https://stackoverflow.com/a/5524862/452614。我可能会更新我的答案以提供完整的解决方案。
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.ext.DefaultHandler2;
class MyHandler extends DefaultHandler2 {
private StringBuilder acc;
public MyHandler() {
acc = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
System.out.printf("startElement. uri:%s, localName:%s, qName:%s\n", uri, localName, qName);
acc.setLength(0);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.printf("endElement. uri:%s, localName:%s, qName:%s\n", uri, localName, qName);
System.out.printf("Characters accumulated: %s\n", acc.toString());
acc.setLength(0);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
acc.append(ch, start, length);
System.out.printf("characters. [%s]\n", new String(ch, start, length));
}
@Override
public void startEntity(java.lang.String name)
throws SAXException {
System.out.printf("startEntity: %s\n", name);
}
@Override
public void endEntity(java.lang.String name)
throws SAXException {
System.out.printf("endEntity: %s\n", name);
}
}
public class SAXTest1 {
public static void main(String args[]) throws SAXException, ParserConfigurationException, UnsupportedEncodingException {
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE author [\n< !ELEMENT author (#PCDATA)>\n<!ENTITY % HTMLlat1 PUBLIC \"-//W3C//ENTITIES Latin 1 for XHTML//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent\"> \n%HTMLlat1;\n]>\n<author>Günther Heinemann</author>";
System.out.println(s);
InputStream stream = new ByteArrayInputStream(s.getBytes("UTF-8"));
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
DefaultHandler2 handler = new MyHandler();
xmlReader.setContentHandler(handler);
xmlReader.setProperty(
"http://xml.org/sax/properties/lexical-handler",
handler);
try {
xmlReader.parse(new InputSource(stream));
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
} catch (SAXException e) {
System.err.println("Parsing error: " + e.getMessage());
}
}
}
程序执行:
$ java SAXTest1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE author [
<!ELEMENT author (#PCDATA)>
<!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "http://www.w3. org/TR/xhtml1/DTD/xhtml-lat1.ent">
%HTMLlat1;
]>
<author>Günther Heinemann</author>
startEntity: %HTMLlat1
endEntity: %HTMLlat1
startElement. uri:, localName:, qName:author
characters. [G]
startEntity: uuml
endEntity: uuml
characters. [ünther Heinemann]
endElement. uri:, localName:, qName:author
Characters accumulated: Günther Heinemann