for循环出错。值未正确显示

时间:2014-03-03 20:31:05

标签: java xml dom

public static void main(String[] args) throws Exception {

    ArrayList<StepAttributesDisplay> attrList = new ArrayList<StepAttributesDisplay>();

    //Get the DOM Builder Factory
    DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();

    //Get the DOM Builder
    DocumentBuilder builder = factory.newDocumentBuilder();

    //Load and Parse the XML document
    //document contains the complete XML as a Tree.
    Document document =
            builder.parse(
                    ClassLoader.getSystemResourceAsStream("xml/input.xml"));
    // for now lets keep one object

    //Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            StepAttributesDisplay attributesDisplay = new StepAttributesDisplay();
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                if (cNode instanceof Element) {
                    NodeList childNodes2 = cNode.getChildNodes();
                    for (int h = 0; h < childNodes2.getLength(); h++) {
                        Node dNode = childNodes2.item(h);
                        // System.out.println(cNode.getNodeName());

                        if (dNode.getNodeName().equals("function")) {
                            System.out.println(dNode.getLastChild().getNodeValue().trim());
                            attrList.add(attributesDisplay);
                            for (int x = 0; x < attrList.size(); x++) {

                                System.out.println("Function New " + attrList.get(x).getFunction());

                            }//attrList.add(attributesDisplay);

                        }
                    }
                }
            }
        }
    }
}

我得到的输出是:

0
Alloc
Alloc
Function New Alloc
Format
Format
Function New Format
Function New Format
Format Check
Format Check
Function New Format Check
Function New Format Check
Function New Format Check
Delete
Delete
Function New Delete
Function New Delete
Function New Delete
Function New Delete
4
0
FunctionDelete
1
FunctionDelete
2
FunctionDelete
3
FunctionDelete

理想情况下应该是:

FunctionAlloc
FunctionFormat
FunctionFormat Check
FunctionDelete

3 个答案:

答案 0 :(得分:0)

我认为XPath是一个比你通过三个深嵌套循环嵌套循环更好的工具。

我无法从你的代码逻辑中准确地说出来,但我猜这是一个 类似于(“// function / text()”)的Xpath表达式将干净地提取所有函数节点值,而不管它们在层次结构中的位置。

对于Java中的XPath基础知识,请参阅How to read XML using XPath in Java 强文

答案 1 :(得分:0)

您的内循环似乎是问题发生的地方。您是否错过了条件检查,您似乎每次打印

if (dNode.getNodeName().equals("function")) {
    System.out.println(dNode.getLastChild().getNodeValue().trim());
    attrList.add(attributesDisplay);
    //==>> this loop below appears to print out all of the addributes every time  it is called
    for (int x = 0; x < attrList.size(); x++) {
      System.out.println("Function New "+ attrList.get(x).getFunction());
}// attrList.add(attributesDisplay);
}

答案 2 :(得分:0)

谢谢你们, 我在错误的地方创建了对象'attributesDisplay'。 这是修改后的代码及其工作原理:)

      NodeList nodeList = document.getDocumentElement().getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++) {
           Node node = nodeList.item(i);
           if (node instanceof Element) {


                  NodeList childNodes = node.getChildNodes();

                  for (int j = 0; j < childNodes.getLength(); j++) {
                      Node cNode = childNodes.item(j);
                      if (cNode instanceof Element) {
                         attributesDisplay= new StepAttributesDisplay();

                          NodeList childNodes2 = cNode.getChildNodes();
                          for (int h = 0; h < childNodes2.getLength(); h++) {
                                Node dNode = childNodes2.item(h);
                               // System.out.println(cNode.getNodeName());

                                if(dNode instanceof Element){
相关问题