需要获取父ID,但我在Java中获得xml标记的级别

时间:2015-01-07 21:39:57

标签: java xml

下面是我的XML

<jndi> 
    <ejbmodule> 
        <context>
            <leaf> 
                <name>XAConnectionFactory</name> 
                <type>type1</type> 
                <year>2002</year>
                <attribute name="class">org.jboss.mq.SpyXAConnectionFactory</attribute> 
                <test1>
                    <name>mytest</name>
                </test1>
            </leaf> 
            <name>java</name> 
            <type>type2</type> 
            <year>2003</year>           
            <leaf> 
                <name>DefaultDS</name> 
                <type>type3</type> 
                <year>2004</year>
                <attribute name="class">javax.sql.DataSource</attribute> 
            </leaf> 
        </context>
    </ejbmodule>    
</jndi>

下面是我的java代码,我将遍历XML并将其转换为DTree javascript代码,以便在html页面上将XML显示为树结构

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class RecursiveDOM {

    static int globalCounter = 0;

    public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException {
        new RecursiveDOM("C:\\Users\\Rajani\\Downloads\\SampleXMLData\\dTree_1\\new1.xml");
    }

    public RecursiveDOM(final String file) throws SAXException, IOException, ParserConfigurationException {
        final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        InputSource is = new InputSource(new FileReader(new File(file)));
        final Document doc = docBuilder.parse(is);
        StringBuffer html = new StringBuffer();
        int parentId = -1;

        html.append("<div class=\"dtree\"><script type=\"text/javascript\" src=\"dtree.js\"></script>\n");
        html.append(createTreeCommandLinks("decisionReportTree"));
        // create tree and add root node
        html.append("<script type=\"text/javascript\">\n<!--\n");
        html.append("decisionReportTree = new dTree('decisionReportTree');\n");
        html.append("decisionReportTree.add(" + globalCounter++ + ",-1,'List Persons');\n");

        traverse(doc, parentId, html);

        html.append("document.write(decisionReportTree);");
        html.append("\n//-->\n</script>");
        System.out.println(html);
    }

    private static String add(String tree, int global, int parent, String name) {
        return tree + ".add(" + global + ", " + parent + ", '" + name + "');\n";
    }

    public static void traverse(Node node, int parentId, StringBuffer html) {
        handle_node(node, parentId, html);
        if (node.hasChildNodes()) {
            NodeList children = node.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node kid = children.item(i);
                if (kid.getNodeType() == Node.ELEMENT_NODE) {
                    traverse(kid, parentId + 1, html);
                }
            }
        }
    }

    public static void handle_node(Node node, int parentId, StringBuffer html) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String value = getNodeValue(node);

            if(null != value && value.trim().length() > 0) {
                html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName() + ":::" + value));
            } else {
                html.append(add("decisionReportTree", globalCounter++, parentId, node.getNodeName()));
            }
        }
    }

    private static String createTreeCommandLinks(String treeName) {
        return "<p><a href=\"javascript: " + treeName + ".openAll();\">Expands all</a> | <a href=\"javascript: " + treeName + ".closeAll();\">Collapse all</a></p>";
    }

    protected static String getNodeValue(Node node) {
        NodeList childNodes = node.getChildNodes();
        for (int x = 0; x < childNodes.getLength(); x++) {
            Node data = childNodes.item(x);
            if (data.getNodeType() == Node.TEXT_NODE)
                return data.getNodeValue();
        }
        return "";
    }

    protected String getNodeAttrValue(String attrName, Node node) {
        NamedNodeMap attrs = node.getAttributes();
        for (int y = 0; y < attrs.getLength(); y++) {
            Node attr = attrs.item(y);
            if (attr.getNodeName().equalsIgnoreCase(attrName)) {
                return attr.getNodeValue();
            }
        }
        return "";
    }
}

我在上述程序中获得的输出是

decisionReportTree = new dTree('decisionReportTree');
decisionReportTree.add(0,-1,'List Persons');
decisionReportTree.add(1, 0, 'jndi');
decisionReportTree.add(2, 1, 'ejbmodule');
decisionReportTree.add(3, 2, 'context');
decisionReportTree.add(4, 3, 'leaf');
decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
decisionReportTree.add(6, 4, 'type:::type1');
decisionReportTree.add(7, 4, 'year:::2002');
decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
decisionReportTree.add(9, 4, 'test1');
decisionReportTree.add(10, 5, 'name:::mytest');
decisionReportTree.add(11, 3, 'name:::java');
decisionReportTree.add(12, 3, 'type:::type2');
decisionReportTree.add(13, 3, 'year:::2003');
decisionReportTree.add(14, 3, 'leaf');
decisionReportTree.add(15, 4, 'name:::DefaultDS');
decisionReportTree.add(16, 4, 'type:::type3');
decisionReportTree.add(17, 4, 'year:::2004');
decisionReportTree.add(18, 4, 'attribute:::javax.sql.DataSource');
document.write(decisionReportTree);

但我需要以下输出

decisionReportTree = new dTree('decisionReportTree');
decisionReportTree.add(0,-1,'List Persons');
decisionReportTree.add(1, 0, 'jndi');
decisionReportTree.add(2, 1, 'ejbmodule');
decisionReportTree.add(3, 2, 'context');
decisionReportTree.add(4, 3, 'leaf');
decisionReportTree.add(5, 4, 'name:::XAConnectionFactory');
decisionReportTree.add(6, 4, 'type:::type1');
decisionReportTree.add(7, 4, 'year:::2002');
decisionReportTree.add(8, 4, 'attribute:::org.jboss.mq.SpyXAConnectionFactory');
decisionReportTree.add(9, 4, 'test1');
decisionReportTree.add(10, 9, 'name:::mytest');
decisionReportTree.add(11, 3, 'name:::java');
decisionReportTree.add(12, 3, 'type:::type2');
decisionReportTree.add(13, 3, 'year:::2003');
decisionReportTree.add(14, 3, 'leaf');
decisionReportTree.add(15, 14, 'name:::DefaultDS');
decisionReportTree.add(16, 14, 'type:::type3');
decisionReportTree.add(17, 14, 'year:::2004');
decisionReportTree.add(18, 14, 'attribute:::javax.sql.DataSource');
document.write(decisionReportTree);

2 个答案:

答案 0 :(得分:0)

问题在于:

traverse(kid, parentId + 1, html);

在您传递parentId + 1的位置,您应该提供您正在遍历其子节点的节点的ID。要获取它,请将handle_node更改为返回它分配给处理节点的ID ...

答案 1 :(得分:0)

此处的错误是为孩子传递traverse(kid, parentId + 1, html)。你正在做的方式是计算节点深度,而不是传输注释父Id。

要获得所需的结果,您必须在每次互动时传递当前和父ID。看一下这个示例类:

public class Example {

    static Node root = new Node("List Persons",
    new Node("jndi",
            new Node("ejbmodule",
                new Node("context",
                    new Node("leaf",
                        new Node("name"),
                        new Node("type"),
                        new Node("year"),
                        new Node("attribute"),
                        new Node("test1", new Node("name"))
                    ),
                    new Node("name"),
                    new Node("type"),
                    new Node("year"),
                    new Node("leaf",
                        new Node("name"),
                        new Node("type"),
                        new Node("year"),
                        new Node("attribute")
                    )
                )
            )
    )
    );

    static int globalCounter = 0;

    public static void main(String[] args) {

        traverse(root, globalCounter++, -1, null);

    }

    private static String add(String tree, int global, int parent, String name) {
        return tree + ".add(" + global + ", " + parent + ", '" + name + "');";
    }

    public static void traverse(Node node, int currentId, int parentId, StringBuffer html) {
        handle_node(node, currentId, parentId, html);
        //if (node.hasChildNodes()) {
            //NodeList children = node.getChildNodes();
            for (Node kid : node.childs) {
                //Node kid = children.item(i);
                //if (kid.getNodeType() == Node.ELEMENT_NODE) {
                    traverse(kid, globalCounter++ , currentId, html);
                //}
            }
        //}
    }

    public static void handle_node(Node node, int currentId, int parentId, StringBuffer html) {
        //if (node.getNodeType() == Node.ELEMENT_NODE) {
            String value = node.value; //getNodeValue(node);

            System.out.println(add("decisionReportTree", currentId, parentId, value ));
            //if(null != value && value.trim().length() > 0) {
            //    html.append(add("decisionReportTree", currentId, parentId, node.getNodeName() + ":::" + value));
            //} else {
            //    html.append(add("decisionReportTree", currentId, parentId, node.getNodeName()));
            //}
        //}
    }
}
class Node {
    String value;
    Node[] childs;

    public Node(String value, Node... childs) {
        this.value = value;
        this.childs = childs;
    }

}

希望有所帮助!