你用布尔变量做什么?跟踪嵌套?
我最近通过对每个元素使用枚举来实现这一点。代码正在工作,但这是我脑海中的粗略近似:
enum Element {
// special markers:
ROOT,
DONT_CARE,
// Element tag parents
RootElement( "root" ROOT),
AnElement( "anelement"), // DONT_CARE
AnotherElement( "anotherelement"),// DONT_CARE
AChild( "child", AnElement),
AnotherChild( "child", AnotherElement);
Element() {...}
Element(String tag, Element ... parents) {...}
}
class MySaxParser extends DefaultHandler {
Map<Pair<Element, String>, Element> elementMap = buildElementMap();
LinkedList<Element> nestingStack = new LinkedList<Element>();
public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) {
Element parent = nestingStack.isEmpty() ? ROOT : nestingStack.lastElement();
Element element = elementMap.get(pair(parent, sName));
if (element == null)
element = elementMap.get(DONT_CARE, sName);
if (element == null)
throw new IllegalStateException("I did not expect <" + sName + "> in this context");
nestingStack.addLast(element);
switch (element) {
case RootElement: ... // Probably don't need cases for many elements at start unless we have attributes
case AnElement: ...
case AnotherElement: ...
case AChild: ...
case AnotherChild: ...
default: // Most cases here. Generally nothing to do on startElement
}
}
public void endElement(String namespaceURI, String sName, String qName) {
// Similar to startElement() but most switch cases do something with the data.
Element element = nestingStack.removeLast();
if (!element.tag.equals(sName)) throw IllegalStateException();
switch (element) {
...
}
}
// Construct the structure map from the parent information.
private Map<Pair<Element, String>, Element> buildElementMap() {
Map<Pair<Element, String>, Element> result = new LinkedHashMap<Pair<Element, String>, Element>();
for (Element element: Element.values()) {
if (element.tag == null) continue;
if (element.parents.length == 0)
result.put(pair(DONT_CARE, element.tag), element);
else for (Element parent: element.parents) {
result.put(pair(parent, element.tag), element);
}
}
return result;
}
// Convenience method to avoid the need for using "new Pair()" with verbose Type parameters
private <A,B> Pair<A,B> pair(A a, B b) {
return new Pair<A, B>(a, b);
}
// A simple Pair class, just for completeness. Better to use an existing implementation.
private static class Pair<A,B> {
final A a;
final B b;
Pair(A a, B b){ this.a = a; this.b = b;}
public boolean equals(Object o) {...};
public int hashCode() {...};
}
}
编辑:
XML 结构中的位置由一堆元素跟踪。当调用 startElement 时,可以通过以下方式确定适当的枚举:1) 跟踪堆栈中的父元素和 2) 作为 sName 参数传递的元素标记作为从定义为枚举Element
的一部分的父信息生成的 Map 的键Element
. 该类Pair
只是两部分密钥的持有者。
这种方法允许在 XML 结构的不同部分重复出现的具有不同语义的相同元素标记由不同的Element
枚举表示。例如:
<root>
<anelement>
<child>Data pertaining to child of anelement</child>
</anelement>
<anotherelement>
<child>Data pertaining to child of anotherelement</child>
</anotherelement>
</root>
使用这种技术,我们不需要使用标志来跟踪上下文,以便我们知道<child>
正在处理哪个元素。上下文被声明为Element
枚举定义的一部分,并通过消除各种状态变量来减少混淆。