我正在尝试将当前文档位置存储在堆栈中,在startElement时进行推送,在endElement时进行弹出。目前我正在使用以下代码:
public void startElement(String namespaceURI, String elname,
String qName, Attributes atts) throws SAXException {
original.append(innerText);
original.append("<");
original.append(elname);
original.append(">");
docStack.push(new StackElement(elname,atts));
....
很不幸,当它尝试在后面读取atts时,会出现错误: Caused by: java.lang.IllegalStateException: Attributes can only be used within the scope of startElement()。
有没有快速可靠的方法来存储这些属性? 此外,是否有比为每个开始标记构建新的自定义对象StackElement更好的方法?
org.xml.sax.helpers.AttributesImpl
替代Map
,它有一个构造函数可以接受一个Attributes
对象。这样做的开销可能是相似的。 - Jörn Horstmann