刚刚得到了一个关于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;
}
这里是我的问题:为什么返回“2” - 陈述从来没有达到?或者让我反问:为什么nodeValue最后仍然是空的?我已经测试过了,代码打印出这个“System.out.println”东西,但从来没有到达那里的回报?! 如果我想让nodeValue填充“else”语句中的值,那么需要更改哪些内容?
编辑:
好吧,我尝试了你什么建议但不幸的是我从来没有得到我想要有结果。 进一步说明:
我有一个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>
我做的方法调用,我告诉你在下面:
searchNode(document.getFirstChild(), null, "id")
然后,我想将子节点<id>
的id-value从<actor>
保存到一个变量中(之后该方法应该以类似于break;
的方式终止,因为我不想获取所有的ID,只需要这个单一的ID。
if'System.out。println(“YESSA:”+ nodeValue);'然后执行'return'2:“+ nodeValue;'也被执行... – assylias 2014-10-29 14:47:31
情况并非如此。 'System.out.println'被执行,但唯一被执行的返回是最后一个不包含'return 2:“+ nodeValue'的函数,最后这个方法返回一个空的值: -/ – 2014-10-29 14:50:49
你从未在'if'情况下使用'searchNode'的返回值... – l4mpi 2014-10-29 14:52:07