您的位置:首页 > 其它

xml文件解析(一)

2016-04-13 17:04 274 查看
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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: