java读取xml信息

来源:互联网 发布:在交换机查ip端口号 编辑:程序博客网 时间:2024/06/05 09:47
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"));}}


0 0
原创粉丝点击