Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java for循环出错。这些值没有正确显示_Java_Xml_Dom - Fatal编程技术网

Java for循环出错。这些值没有正确显示

Java for循环出错。这些值没有正确显示,java,xml,dom,Java,Xml,Dom,理想情况下应该是: 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 De

理想情况下应该是:

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

我认为XPath对于您的任务来说是一个比通过三层嵌套循环进行嵌套循环更好的工具

我无法从您的代码逻辑中准确地判断,但我猜 类似//function/text的Xpath表达式将干净地提取所有函数节点值,而不管它们在层次结构中的位置如何


有关Java中的XPath基础知识,请参阅strong text

您的内部循环似乎就是问题发生的地方。如果您缺少一个条件检查,您似乎正在打印每个

FunctionAlloc
FunctionFormat
FunctionFormat Check
FunctionDelete
谢谢各位, 我在错误的位置创建了对象“attributesDisplay”。 这是修改后的代码及其工作原理:

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);
}

那个xml文件看起来像什么?为什么要嵌套四个for循环?是否可以将其中的一些函数重构为单独的函数?注释这一行System.out.printlndNode.getLastChild.getNodeValue.trim;那么代码到底应该做什么呢?你必须给我们更多的东西继续下去。重构那只野兽,然后试着找出问题所在!
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);
}
      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){