`

VTD-XML技术解析XML实例

阅读更多

VTD-XML是一种无提取的XML解析方法,它较好的解决了DOM占用内存过大的缺点,并且还提供了快速的解析与遍历、对XPath的支持和增量更新等特性。VTD-XML是一个开源项目,目前有Java、C两种平台支持。

由于VTD-XML选取特定节点的遍历,修改都有很好的简单实例,这里就不再举例了。

下面的实例是尝试遍历整个XML的节点,还不是很完善。

 

 遍历XML的所有节点

 

public static void travelAll(VTDNav vn) throws Exception {
        BookMark bm = new BookMark();
        vn.toElement(VTDNav.ROOT);
        // assume vn is already available
        AutoPilot ap = new AutoPilot(vn); // ap is going to move the cursor
        ap.selectElement("*"); // select every element (* matches all strings)
        while (ap.iterate()) // iterate will iterate thru all elements
        {
            // put processing logic here
            System.out.println("Element name ==> " + vn.toString(vn.getCurrentIndex()));
            int t = vn.getText();
            if (t != -1) System.out.println("Text content ==> " + vn.toNormalizedString(t));

            int attrIndex = vn.getAttrVal("partNum");
            if (attrIndex != -1) {
                System.out.println("attrValue----------" + vn.toString(attrIndex));
            }

            // make sure that the cursor position entering the loop is the same
            // as the position exiting the loop
            // One way to do is use teh combination of vn.push() and vn.pop()
            // The other is to manually move the cursor back into the original place
        }
        vn.toElement(VTDNav.ROOT); // reset the cursor to point to the root element
    }

 

 遍历XML所有节点的属性

 

public static void travelAllAttribute(VTDNav vn) throws Exception {
        String rootName = vn.toString(vn.getRootIndex());
        System.out.println(rootName);
        if (vn.toElement(VTDNav.ROOT)) {
            int r0 = vn.getCurrentIndex();
            if (r0 != -1) {
                System.out.println("rootName---------- " + vn.toString(r0));
            }

            AutoPilot ap = new AutoPilot(vn);
            ap.selectAttr("*");
            int attrCount = vn.getAttrCount();
            for (int i = 0; i < attrCount; i++) {
                int a = ap.iterateAttr();
                if (a != -1) {
                    System.out.println("attrName---------- " + vn.toString(a));
                }
            }

            while (vn.toElement(VTDNav.FIRST_CHILD)) {
                while (vn.toElement(VTDNav.NEXT_SIBLING)) {
                    int c = vn.getCurrentIndex();
                    if (c != -1) {
                        System.out.println("first child name----------- " + vn.toString(c));
                    }
                    int f = vn.getText();
                    if (f != -1) {
                        System.out.println("first child value---------- " + vn.toString(f));
                    }

                    ap.selectAttr("*");
                    attrCount = vn.getAttrCount();
                    for (int i = 0; i < attrCount; i++) {
                        int attrIndex = ap.iterateAttr();
                        if (attrIndex != -1) {
                            String attrName = vn.toString(attrIndex);
                            System.out.println("attrName---------- " + attrName);
                            int attrValueIndex = vn.getAttrVal(attrName);
                            if (attrValueIndex != -1) {
                                String attrValue = vn.toString(attrValueIndex);
                                System.out.println("attrValue---------- " + attrValue);
                            }
                        }
                    }
                }

            }

            if (vn.toElement(VTDNav.FIRST_CHILD)) {
                int c = vn.getCurrentIndex();
                if (c != -1) {
                    System.out.println("first child name-----------" + vn.toString(c));
                }
                int f = vn.getText();
                if (f != -1) {
                    System.out.println("first child ------" + vn.toString(f));
                }

                int d = vn.getCurrentDepth();
                System.out.println(d);

                if (vn.toElement(VTDNav.FIRST_CHILD)) {
                    int i = vn.getCurrentIndex();
                }
            }

            if (vn.toElement(VTDNav.NEXT_SIBLING)) {
                int c = vn.getCurrentIndex();
                if (c != -1) {
                    System.out.println("first child name-----------" + vn.toString(c));
                }
                int f = vn.getText();
                if (f != -1) {
                    System.out.println("first child ------" + vn.toString(f));
                }

                int d = vn.getCurrentDepth();
                System.out.println(d);

                if (vn.toElement(VTDNav.FIRST_CHILD)) {
                    c = vn.getCurrentIndex();
                    if (c != -1) {
                        System.out.println("first child name-----------" + vn.toString(c));
                    }
                    f = vn.getText();
                    if (f != -1) {
                        System.out.println("first child ------" + vn.toString(f));
                    }

                    d = vn.getCurrentDepth();
                    System.out.println(d);

                    ap.selectAttr("*");
                    attrCount = vn.getAttrCount();
                    for (int i = 0; i < attrCount; i++) {
                        int a = ap.iterateAttr();
                        if (a != -1) {
                            System.out.println("attrName---------- " + vn.toString(a));
                            int t = vn.getAttrVal(vn.toString(a));
                            System.out.println("attrValue---------- " + vn.toString(t));
                        }
                    }

                    if (vn.toElement(VTDNav.FIRST_CHILD)) {
                        int i = vn.getCurrentIndex();
                    }
                }
            }
        }
    }

 

 VTD-XML  API文档 VTD-XML_intro.zip在附件中

参考:

http://blog.csdn.net/jamesmf/article/details/7769730

http://www.javaworld.com/article/2071745/soa/simplify-xml-processing-with-vtd-xml.html

http://www.codeproject.com/Articles/28237/Programming-XPath-with-VTD-XML

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics