public void analysXML() throws Exception {
InputStream stream =this.getClass().getResourceAsStream(
"/myxml.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder = factory.newDocumentBuilder();
Document doc = dbuilder.parse(stream);
Element root = doc.getDocumentElement();
NodeList nodeList = root.getChildNodes();
if (nodeList.getLength() == 0)return;
for (int i = 0; i < nodeList.getLength(); i++) {
Node item = nodeList.item(i);
if (item.getNodeName().equals("#text")// 此类节点是在解析XML文件时产生的并非定义的,是必须要加的判断
|| item.getNodeName().isEmpty()
|| item.getNodeName() ==null) {
// System.out.println(item.getNodeName());
continue;
}
//解析ver的值
if("ver".equals(item.getNodeName()){
String version = item.getNodeValue();
}
//这个方法可以取到ver的属性
NamedNodeMap nnm = item.getAttributes();
}
}
标签:java,xml,解析