java解析xml

 List<String> list = new ArrayList<String>();

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
File file = new File("src/configue.xml");
try {
 
DocumentBuilder db = dbf.newDocumentBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "utf-8"));
InputSource is = new InputSource(in);
Document doc = db.parse(is);
NodeList nodeList = doc.getElementsByTagName("ZoneTarget");
for (int i = 0; i < nodeList.getLength(); i++) {
Node fatherNode = nodeList.item(i);
NodeList childList = fatherNode.getChildNodes();
boolean flag = false;
for (int j = 0; j < childList.getLength(); j++) {
Node childNode = childList.item(j);
if (childNode.getNodeName().equals("zoneId")
&& childNode.getTextContent().equals(zoneId)) {
flag = true;
break;
}
}
if (flag) {
for (int j = 3; j < childList.getLength(); j+=2) {
Node childNode = childList.item(j);
list.add(childNode.getTextContent());
}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return list; 
}

本文出自 “艰难困苦 玉汝于成” 博客,谢绝转载!

你可能感兴趣的:(java,xml,职场,休闲)