简体   繁体   English

Sax解析最佳实​​践

[英]Sax parsing best practices

What are the best ways to minimize the pain of implementing a sax parser to build an object tree? 什么是最小化实现萨克斯解析器构建对象树的痛苦的最佳方法? Like, in what method do you create java objects and where do you store them, how do you keep and use a stack for complex hierarchies, how do you handle attributes, and how do you handle nested elements? 比如,在什么方法中创建java对象以及在哪里存储它们,如何保存和使用堆栈用于复杂的层次结构,如何处理属性,以及如何处理嵌套元素? How do you handle character data? 你如何处理角色数据?

Basically, where does core functionality fit to make life easiest? 基本上,核心功能在哪里适合让生活变得最简单?

I frequently use Dom4j's SAXReader with an ElementHandler to build mini-DOM chunks of a large XML file . 我经常使用Dom4j的SAXReaderElementHandler构建大型XML文件的迷你DOM块

There are a few things to keep in mind: 有几点需要注意:

  1. Call elementPath.getCurrent() to get the actual element in your onEnd() . 呼叫elementPath.getCurrent()来获得实际的元素在你的onEnd()
  2. Remember to call element.detach() at the end of your onEnd() method. 记得在onEnd()方法的末尾调用element.detach()
  3. The path you give to addHandler(String, Handler) is NOT an xpath: it's just a '/' -separated Path stack . 您为addHandler(String, Handler)提供的路径不是xpath:它只是一个'/'分隔的Path堆栈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM