java--xml解析

private static void forEach(String filepath) throws Exception {
		//获取解析器工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//获取解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		//解析文档
		Document doc = builder.parse(filepath);
		//获取根节点
		Element root = doc.getDocumentElement();
		//遍历节点
		forEachDetail(root);
	}

	private static void forEachDetail(Node node){
		NodeList nodeList = node.getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node childNode = nodeList.item(i);
			System.out.println(childNode.getNodeName());
			forEachDetail(childNode);
		}
	}

你可能感兴趣的:(java--xml解析)