zoukankan      html  css  js  c++  java
  • jdk阅读xml文件

    前言

    你需要阅读的时间来写一个通用组件xml文件,但考虑到组件分布更容易,这样一来在第三方小引用jar包。因此,直接jdk内建的xml分析方法。可能都没有第三发的组件强大。

    导入的文件:

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

    解析代码:

    public Map loadUrlRange() {
    		Map map = new HashMap();
    		try {
    			java.net.URL url = getClass().getResource("/");
    			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 获取一个DocumentBuilderFactory的实例
    			DocumentBuilder db = dbf.newDocumentBuilder(); // 使用工厂生成一个DocumentBuilder
    			File file = new File(url.getFile() + "/orgClientRes/urlRange.xml"); // 打开文件,获得句柄
    			Document doc = db.parse(file); // 使用dom解析xml文件
    
    
    			NodeList urlList = doc.getElementsByTagName("url"); // 将全部节点名为product的节点取出
    			Element productElement; // 元素对象。声明
    			for (int i = 0; i < urlList.getLength(); i++) // 循环处理对象
    			{
    				productElement = (Element) urlList.item(i); 
    				String doUrl = productElement.getAttribute("doUrl"); 
    				// System.out.println("链接: " + doUrl);
    				if(!map.containsKey(doUrl)){
    					map.put(doUrl, "");
    				}
    				
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return map;
    	} 


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    JAVA基础——编程练习(二)
    JAVA基础——面向对象三大特性:封装、继承、多态
    JVM内存
    50. Pow(x, n) (JAVA)
    47. Permutations II (JAVA)
    46. Permutations (JAVA)
    45. Jump Game II (JAVA)
    43. Multiply Strings (JAVA)
    42. Trapping Rain Water (JAVA)
    41. First Missing Positive (JAVA)
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4634881.html
Copyright © 2011-2022 走看看