XML 파일을 통해 구문 분석 중이므로 작동하는 자식 노드를 가져옵니다. 간단히 String에 넣고 for 루프에서 전달하려고하지만, 전달하면 String에 모든 자식 노드를 넣지 않고 String에 아무것도 넣지 않는 경우가 있습니다. if 문 아래에서 System.out.println (data)을 사용하면 제대로 작동합니다. if 루프에서 데이터를 가져 와서 전달하려고합니다. 여기에 내 코드 ...루프를 중첩 된 try-catch에서 문자열을 추출 할 수 없습니다.
public class Four {
public static void main(String[] args) {
Four four = new Four();
String a = null;
try {
Document doc = four.doIt();
String b = four.getString(doc);
System.out.println(b);
} catch (Exception e) {
e.printStackTrace();
}
}
public Document doIt() throws IOException, SAXException, ParserConfigurationException {
String rawData = null;
URL url = new URL("http://feeds.cdnak.neulion.com/fs/nhl/mobile/feeds/data/20140401.xml");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
return doc;
}
public String getString(Document doc) {
String data = null;
NodeList cd = doc.getDocumentElement().getElementsByTagName("game");
for (int i = 0; i < cd.getLength(); i++) {
Node item = cd.item(i);
System.out.println("==== " + item.getNodeName() + " ====");
NodeList children = item.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node child = children.item(j);
if (child.getNodeType() != Node.TEXT_NODE) {
data = child.getNodeName().toString() + ":" + child.getTextContent().toString();
// if I System.out.println(data) here, it shows all everything
I want, when I return data it shows nothing but ===game===
}
}
}
return data;
}
}
값을 반환 할 때와 호출자가 값을 가져올 때 사이에 값을 변경해서는 안되기 때문에 디버거를 사용하여 디버깅을 시도 할 수 있습니까? –