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;
}
}
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;
}
}
相关文章推荐
- iOS开发:MJExtension的简单使用
- iOS网络编程1--简介
- 移动端前端UI库—Frozen UI、WeUI、SUI Mobile
- Java中的int和Integer
- 16进制颜色表
- 消息队列中点对点与发布订阅区别
- UML常用图的几种关系的总结
- -stdcall详解
- Eclipse使用技巧 - 4. Eclipse自动注释文件的导出和导入
- Look-alike技术运用实例:有效实现目标粉丝的“爆炸”
- xml文件解析的几种方式
- Java集合(三):Queue队列
- OpenStack安装时Compute节点连不上RabbitMQ
- link和@import两种导入css文件的区别。
- phpstudy APACHE支持.htaccess以及 No input file specified解决方案
- HDFS源码分析之数据块Block、副本Replica
- 为何使用消息系统
- 第六周 用多文件组织多个类的程序(带武器的角色类)
- c++通过ADO方式操作SQLSERVER数据库
- 申论备考攻略:议论文中两大论证方法