开发者

Specify XML path in dom4j

I want to parse a large xml file using dom4j. I'm using the dom4j's feature that you can register event handlers for path expressions for ignoring the elements I don't care about. The feature is explained here: http://dom4j.sourceforge.net/dom4j-1.6.1/faq.html#large-doc.

I quote from there: "These handlers will then be called on the start and end of each path registered against a particular handler. When the start tag of a path is found, the onStart method of the handler registered to the path is called. When the end tag of a path if found, the onEnd method of the handler registered to that path is called.

The onStart and onEnd methods are passed an instance of an ElementPath, which can be used to retrieve the current Element for the given path. If the handler wishes to "prune" the tree being built in order to save memory use, it can simply call the detach() method of the current Element being processed in the handlers onEnd() method."

My problem is that I don't know what path should I give so that all the children of the root node to be handled by the 2 methods.

My xml file is something like:

<root .....>
  <chef name="" ..../>
  <chef name="" ..../>
  <recipe name = .... />
  <recipe name...../>
  ....

If I would like to handle chef elements than the path would be /root/chef. For recipe elements the path would be /root/recipe.

But what is the path that should be given to the dom4j so that it will handle (in the onStart(), onEnd()) both chef 开发者_开发技巧and recipe elements?

Thanks a lot!


Instead of calling the addHandler() method, call the setDefaultHandler() and use it like this:

SAXReader reader = new SAXReader();
reader.setDefaultHandler(
new ElementHandler() {
    public void onStart(ElementPath path) {
        // If needed, similar to onEnd, but don't detach.    
    }
    public void onEnd(ElementPath path) {
        Element parent = path.getCurrent().getParent();
        if(parent != null && "/root".equals(parent.getPath()) {
            // Do whatever
        }

        path.getCurrent().detach();
    }
}
);


Try //root/child::* or //root/descendant::* depending on what level of depth you want.

see w3schools for more on the available xpath axes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜