简体   繁体   English

无法到达的退货声明?

[英]Unreachable Return-Statement?

Just got a question concerning a return statement in Java and the use of a method which should return a value from deep within a xml-statement. 刚遇到一个有关Java中的return语句以及应使用xml语句深处返回值的方法的问题。

public static String searchNode(Node node, Node parent, String nodeResult) {
    String nodeValue = "";

    if (node.hasChildNodes()) {
        NodeList childrens = node.getChildNodes();
        for (int i = 0; i < childrens.getLength(); i++) {
            searchNode(childrens.item(i), node, nodeResult);
        }
    } else {
        nodeValue = node.getNodeValue().trim();
        if (nodeValue.length() > 0) {
            if (parent.getNodeName().equals(nodeResult)) {
                System.out.println("YESSA: " + nodeValue);
                return "2: " + nodeValue;
            }
        }
    }
    return nodeValue;
}

And here's my question: Why is the return "2"-Statement never reached? 这是我的问题:为什么从未返回“ 2”声明? Or let me rather ask: why is nodeValue at the end still empty? 还是让我宁愿问:为什么最后的nodeValue仍然为空? I tested it already and the code prints this "System.out.println"-thing, but never reaches the return there?! 我已经对其进行了测试,并且代码会打印出此“ System.out.println”内容,但从未到达那里? What do I have to change if I wanted the nodeValue filled in the value from the "else"-statement? 如果我想让nodeValue填写“ else”语句中的值,该怎么办?

EDIT: 编辑:

Ok, I tried out what you proposed but unfortunately I never get the result I wanted to have. 好的,我尝试了您的建议,但是很遗憾,我没有得到想要的结果。 Further description: 进一步说明:

I have a xml-structure which looks like this: 我有一个看起来像这样的xml结构:

<?xml version="1.0" encoding="UTF-8" ?>
<log>
<published>2014-03-28T15:28:36.646Z</published>
<actor>
    <objectType>person</objectType>
    <id>e1b8948f-321e-78ca-d883-80500aae71b5</id>
    <displayName>anonymized</displayName>
</actor>
<verb>update</verb>
<object>
    <objectType>concept</objectType>
    <id>a1ad6ace-c722-ffa9-f58e-b4169acdb4e3</id>
    <content>time</content>
</object>
<target>
    <objectType>conceptMap</objectType>
    <id>4b8f69e3-2914-3a1a-454e-f4c157734bd1</id>
    <displayName>my first concept map</displayName>
</target>
<generator>
    <objectType>application</objectType>
    <url>http://www.golabz.eu/content/go-lab-concept-mapper</url>
    <id>c9933ad6-dd4a-6f71-ce84-fb1676ea3aac</id>
    <displayName>ut.tools.conceptmapper</displayName>
</generator>
<provider>
    <objectType>ils</objectType>
    <url>http://graasp.epfl.ch/metawidget/1/b387b6f</url>
    <id>10548c30-72bd-0bb3-33d1-9c748266de45</id>
    <displayName>unnamed ils</displayName>
</provider>
</log>

I do the method call as I show you in the following: 我将在下面向您展示方法调用:

searchNode(document.getFirstChild(), null, "id")

Then, I want to save the id-value of the subnode <id> from <actor> in a variable (after that the method should terminate with something like a break; , because I don't want to get all ID's. Just this single ID. 然后,我想将<actor>中子节点<id>的id值保存在变量中(此后,该方法应以break;类的方法终止,因为我不想获取所有ID。单一ID。

You don't return the result of your recursing. 您不返回递归结果。 You should return it, or set the result. 您应该返回它,或设置结果。

return searchNode(childrens.item(i), node, nodeResult);

or nodeValue = searchNode(childrens.item(i), node, nodeResult); 或nodeValue = searchNode(childrens.item(i),node,nodeResult);

You wrote a recursive method that ignores results of intermediate invocations. 您编写了一个递归方法,该方法忽略了中间调用的结果。 The return statement in question would be reached only if you start your search right at the node that you want to find. 仅当您直接在要查找的节点上开始搜索时,才会返回有问题的return语句。

To fix this, your recursive branch should pay attention to what's returned by recursive invocation. 要解决此问题,您的递归分支应注意递归调用返回的内容。 If you need to find the first matching child, do this: 如果您需要找到第一个匹配的孩子,请执行以下操作:

for (int i = 0; i < childrens.getLength(); i++) {
    String tmp = searchNode(childrens.item(i), node, nodeResult);
    if (tmp != null && tmp.length() != 0) {
        return tmp;
    }
}

You are calling the method recursively, and each call creates its own nodeValue on the stack. 您正在递归地调用该方法,并且每次调用都会在堆栈上创建自己的nodeValue Consider this: 考虑一下:

  • first you call it with the node that has one child, nodeValue is empty string 首先,您使用具有一个子节点的节点来调用它, nodeValue为空字符串
  • then, in the first for loop you call the same method again, new nodeValue is created on the stack 然后,在第一个for循环中,再次调用相同的方法,在堆栈上创建新的nodeValue
  • you return 2: something from it 您返回2: something从中得到2: something
  • you do nothing with that return result, just return empty string in the original call 您对返回结果不执行任何操作,只需在原始调用中返回空字符串

Now, you probably want to use the result from recursive calls, so try this in the for loop 现在,您可能想使用递归调用的结果,因此请在for循环中尝试

nodeValue = searchNode(childrens.item(i), node, nodeResult);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM