3

这纯粹是一个代码可读性相关的问题,类的性能不是问题。

这是我构建这个 XMLHandler 的方式:

对于与应用程序相关的每个元素,我在“ElementName”中有一个布尔值,我在解析过程中根据我的位置将其设置为 true 或 false:问题,我现在在课堂开始时有 10+ 个布尔声明,它越来越大。

在我的 startElement 和 endElement 方法中,我有数百行

if (qName = "elementName") {
   ...
} else if (qName = "anotherElementName") {
   ...
}

其中包含不同的解析规则(如果我在 xml 文件中处于此位置,请执行此操作,否则,请执行此操作等...)

编写新的解析规则和调试变得越来越痛苦。

编写 sax 解析器的最佳实践是什么,我可以做些什么来使我的代码更具可读性?

4

3 回答 3

2

你用布尔变量做什么?跟踪嵌套?

我最近通过对每个元素使用枚举来实现这一点。代码正在工作,但这是我脑海中的粗略近似:

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枚举定义的一部分,并通过消除各种状态变量来减少混淆。

于 2010-08-26T11:03:27.673 回答
0

这取决于 XML 结构。如果针对不同情况的操作很容易或(或多或少)“独立”,您可以尝试使用地图:

interface Command {
   public void assemble(Attributes attr, MyStructure myStructure);
}
...

Map<String, Command> commands= new HashMap<String, Command>();
...
if(commands.contains(qName)) {
   commands.get(qname).assemble(attr, myStructur);
} else {
   //unknown qName
}
于 2010-08-26T10:00:55.893 回答
0

我会回退到 JAXB 或类似的东西,让框架完成工作。

于 2010-08-26T10:08:16.030 回答