src/org/nutz/lang/Xmls.java
public static void eachChildren(Element ele,
                                    String regex,
                                    final Each<Element> callback,
                                    int off) {
        if (null == ele || null == callback)
            return;

        // 正则式
        final Pattern p = null == regex ? null : Pattern.compile(regex);

        NodeList nl = ele.getChildNodes();

        // 循环子
        final int len = nl.getLength();

        // 每次循环执行
        Callback2<Integer, Node> eachInvoke = new Callback2<Integer, Node>() {
            public void invoke(Integer index, Node nd) {
                if (nd instanceof Element)
                    try {
                        Element tag = (Element) nd;
                        if (null == p || p.matcher(tag.getTagName()).find())
                            callback.invoke(index, tag, len);
                    }
                    catch (ExitLoop e) {
                        throw Lang.wrapThrow(e);
                    }
                    catch (ContinueLoop e) {}
                    catch (LoopException e) {
                        throw Lang.wrapThrow(e);
                    }
            }
        };

        try {
            // 负向迭代
            if (off < 0) {
                for (int i = len + off; i >= 0; i--) {
                    eachInvoke.invoke(i, nl.item(i));
                }
            }
            // 正向迭代
            else {
                for (int i = off; i < len; i++) {
                    eachInvoke.invoke(i, nl.item(i));
                }
            }
        }
        catch (ExitLoop e) {}
        catch (RuntimeException e) {
            if (e.getCause() instanceof ExitLoop)
                return;
            else
                throw e;
        }
    }
public static void eachChildren(Element ele,
                                    String regex,
                                    final Each<Element> callback,
                                    int off) {
        if (null == ele || null == callback)
            return;

        // 正则式
        final Pattern p = null == regex ? null : Pattern.compile(regex);

        NodeList nl = ele.getChildNodes();

        // 循环子
        final int len = nl.getLength();

        // 每次循环执行
        Callback2<Integer, Node> eachInvoke = new Callback2<Integer, Node>() {
            public void invoke(Integer index, Node nd) {
                if (nd instanceof Element)
                    try {
                        Element tag = (Element) nd;
                        if (null == p || p.matcher(tag.getTagName()).find())
                            callback.invoke(index, tag, len);
                    }
                    catch (ExitLoop e) {
                        throw Lang.wrapThrow(e);
                    }
                    catch (ContinueLoop e) {}
                    catch (LoopException e) {
                        throw Lang.wrapThrow(e);
                    }
            }
        };

        try {
            // 负向迭代
            if (off < 0) {
                for (int i = len + off; i >= 0; i--) {
                    eachInvoke.invoke(i, nl.item(i));
                }
            }
            // 正向迭代
            else {
                for (int i = off; i < len; i++) {
                    eachInvoke.invoke(i, nl.item(i));
                }
            }
        }
        catch (ExitLoop e) {}
        catch (RuntimeException e) {
            if (e.getCause() instanceof ExitLoop)
                return;
            else
                throw e;
        }
    }
public static void eachChildren ( Element ele , String regex , final Each < Element > callback , int off ) { if ( null == ele || null == callback ) return ; // 正则式 final Pattern p = null == regex ? null : Pattern . compile ( regex ) ; NodeList nl = ele . getChildNodes ( ) ; // 循环子 final int len = nl . getLength ( ) ; // 每次循环执行 Callback2 < Integer , Node > eachInvoke = new Callback2 < Integer , Node > ( ) { public void invoke ( Integer index , Node nd ) { if ( nd instanceof Element ) try { Element tag = ( Element ) nd ; if ( null == p || p . matcher ( tag . getTagName ( ) ) . find ( ) ) callback . invoke ( index , tag , len ) ; } catch ( ExitLoop e ) { throw Lang . wrapThrow ( e ) ; } catch ( ContinueLoop e ) { } catch ( LoopException e ) { throw Lang . wrapThrow ( e ) ; } } } ; try { // 负向迭代 if ( off < 0 ) { for ( int i = len + off ; i >= 0 ; i -- ) { eachInvoke . invoke ( i , nl . item ( i ) ) ; } } // 正向迭代 else { for ( int i = off ; i < len ; i ++ ) { eachInvoke . invoke ( i , nl . item ( i ) ) ; } } } catch ( ExitLoop e ) { } catch ( RuntimeException e ) { if ( e . getCause ( ) instanceof ExitLoop ) return ; else throw e ; } }
迭代 XML 元素内名字符合一个正则表达式的子元素

@param ele
XML 元素
@param regex
元素名称正则表达式
@param callback
回调
@param off
偏移量。0 表示从第一个迭代。 -1 表示从最后一个迭代。-2表示从倒数第二个迭代
迭代 XML 元素内名字符合一个正则表达式的子元素