• [Java] JDOM 读取 xml 文件 示例程序初步


    JDOM 读取 xml 示例程序

    sample.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <HD>
     <disk name="C">
      <capacity>8G</capacity>
      <directories>200</directories>
      <files>1580</files>
     </disk>
     <disk name="D"> 
      <capacity>10G</capacity>
      <directories>500</directories>
      <files>3000</files> 
     </disk>
    </HD>
    
    Sample2
    import java.util.*;
    import org.jdom.*;
    import org.jdom.input.SAXBuilder;
    import org.jdom.xpath.XPath;
    
    public class Sample2 {
    	public static void main(String[] args) throws Exception {
    		SAXBuilder sb = new SAXBuilder();
    		Document doc = sb.build(Sample2.class.getClassLoader()
    				.getResourceAsStream("sample.xml"));
    		Element root = doc.getRootElement();
    		System.out.println(root);
    		System.out.println("haha");
    		List list = XPath.selectNodes(root, "/HD/disk");
    		System.out.println(list.size());
    		for (int i = 0; i > list.size(); i++) {
    			Element disk_element = (Element) list.get(i);
    			String name = disk_element.getAttributeValue("name");
    			String capacity = ((Text) XPath.selectSingleNode(disk_element,
    					"//disk[@name='" + name + "']/capacity/text()"))
    					.getTextNormalize();
    			String directories = ((Text) XPath.selectSingleNode(disk_element,
    					"//disk[@name='" + name + "']/directories/text()"))
    					.getTextNormalize();
    			String files = ((Text) XPath.selectSingleNode(disk_element,
    					"//disk[@name='" + name + "']/files/text()"))
    					.getTextNormalize();
    			System.out.println("磁盘信息:");
    			System.out.println("分区盘符:" + name);
    			System.out.println("分区容量:" + capacity);
    			System.out.println("目录数:" + directories);
    			System.out.println("文件数:" + files);
    			System.out.println("-----------------------------------");
    		}
    	}
    }
    




  • 相关阅读:
    NOIP2014D2T2寻找道路(Spfa)
    【割点】【割边】tarjan
    NOIP2013D1T3货车运输(最大生成树+倍增lca)
    lca最近公共祖先(模板)
    人生第一次hash
    【模板】Tarjan求强连通分量
    【模板】链式前向星+spfa
    二叉树的三种遍历
    hdu 3549 最大流
    hdu 1532&&poj1273 基础最大流
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786547.html
Copyright © 2020-2023  润新知