java读取xml信息

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlUtil {
	static String sss;

	public static String readXML(String file) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = dbf.newDocumentBuilder();
		Document doc = builder.parse(file); // 获取到xml文件

		// 下面开始读取
		Element root = doc.getDocumentElement(); // 获取根元素
		NodeList students = root.getElementsByTagName("weibo-id");
		for (int i = 0; i < students.getLength(); i++) {
			Element ss = (Element) students.item(i);
			NodeList names = ss.getElementsByTagName("id");
			Element e = (Element) names.item(0);
			Node t = e.getFirstChild();
			sss = t.toString();
		}
		return sss;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(readXML("src/main/resources/weibo-config.xml"));
	}
}


你可能感兴趣的:(xml)