xml文件解析(一)

74 篇文章 0 订阅
package com.fourinone;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


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


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;




public class XmlFileParse0 {


public Map<String,Map<String,String>> readTasks(String fileName) throws IOException{     //定义函数,函数中传入的参数为一文件地址,函数的类型为嵌套的Map集合

Map<String,Map<String,String>> tasks = new HashMap<String,Map<String, String>>();  //新建嵌套的HashMap集合
//<Group,<Items,DataType>>
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();    //从 XML 文档获取生成 DOM 对象树的解析器

try{
DocumentBuilder builder = dbf.newDocumentBuilder();    //使用当前配置的参数创建一个新的 DocumentBuilder 实例。
//InputStream in = XmlFileParse.class.getClassLoader().getResourceAsStream("MapTest.xml");
InputStream in = new FileInputStream(fileName);    //文件输入
Document doc = builder.parse(in);                 //将给定 URI 的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象。        

// root <Task>
Element root = doc.getDocumentElement();       //获取根节点

  if(root != null){
  
// all group node  
        NodeList groupNodes = root.getChildNodes();    //获取一级节点Group
        if(groupNodes != null){
        for(int i = 0; i < groupNodes.getLength(); i++){    //对Group节点遍历
        //each group
        Node groupNode = groupNodes.item(i);             //获取Group节点的每一项
        if(groupNode != null && groupNode.getNodeType() == Node.ELEMENT_NODE){     //ELEMENT_NODE是一个枚举值,代表元素节点类型。
        System.out.println("Group"+groupNode.getAttributes().getNamedItem("name").getNodeValue()+":"+"  "+groupNode.getAttributes().getNamedItem("Type").getNodeValue());   //输出组名
        //all tasks
        NodeList taskNodes = groupNode.getChildNodes();               //得到二级节点tagNodes
        Map<String,String> tagList = new HashMap<String,String>();   //新建HashMap集合
         
        if(taskNodes != null){                                        //二级节点tag不为空
        for (int j = 0; j < taskNodes.getLength(); j++){          //对二级节点遍历
        //each task
        Node task = taskNodes.item(j);                         //得到二级节点中的每一项
        if(task != null && task.getNodeType() == Node.ELEMENT_NODE){         //二级节点中的每一项都不为空,数据类型为元素类型
//         System.out.println(tag.getAttributes().getNamedItem("Name").getNodeValue() + "-----------" + tag.getAttributes().getNamedItem("Type").getNodeValue());
        tagList.put(task.getAttributes().getNamedItem("Name").getNodeValue(), task.getAttributes().getNamedItem("Type").getNodeValue());  //分别将每个数据的数据项和数据类型赋给tagList集合的K和V值。
        }
        }
        }
         
        Set<String> keys=tagList.keySet();        //得到全部的key  
        Iterator<String>iter=keys.iterator();     //实例化Itetator
        while(iter.hasNext()){                    //迭代输出全部的key
        String str =iter.next();              //取出集合的key
        @SuppressWarnings("unused")
        String value = tagList.get(str);
        System.out.println("    "+"Key: "+ str + "       " + "Value: " + value);   //输出内容key、values
        }
        System.out.println("");
        tasks.put(groupNode.getAttributes().getNamedItem("name").getNodeValue(), tagList);     //两层嵌套Map,外层是F15,内层是数据项和数据类型。
        }
        }
         }
  }

}catch(ParserConfigurationException e){
e.printStackTrace();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return tasks;
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值