xml解析类,不用任何工具、配置和类库,一个类得到你想要的
2005-12-21 12:55
489 查看
xml文件例子:
<Menu>
<MenuNode ID="010000" dd="db">
<Caption>系统管理</Caption>
<MenuFlag>1</MenuFlag>
<SubNode ID="010100">
<Caption>职工信息</Caption>
<ID_Model>000001</ID_Model>
<Button ID="010101">
<Caption>录入</Caption>
<ID_Model>F010101</ID_Model>
</Button>
<Button ID="010102">
<Caption>修改</Caption>
<ID_Model>F010102</ID_Model>
<hua HUA="my god"></hua>
</Button>
<Parameter INDEX="1">
<TYPE>INT</TYPE>
</Parameter>
</SubNode>
</MenuNode>
</Menu>
----------------------------------------------------------------
解析类:
/*
* Created on 2005-12-15
*/
package com.yx.tool.xml;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author LHH
* xml文件解析工具类,可以根据一个元素名称和索引位置
* 返回这个元素所包含的指定名称子元素信息的一个ArrayList列表
* 子元素信息被封装一个HashMap里,HashMap可以定制以下内容:
* 该子元素名称、属性和它下一层指定名称子元素的值和属性
*/
public class YxXmlParser {
static Element root = null;
public YxXmlParser() {
try {
if (root == null) {
System.out.println("roo is builder!");
File f = new File(
"D:/YSGL/TestTag/src/com/yx/tool/xml/data_10k.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
root = doc.getDocumentElement();
}
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//获取第一层菜单的列表,别修改了,经测试是稳定的
public ArrayList getNameList() {
ArrayList reList = new ArrayList();
NodeList list = root.getElementsByTagName("MenuNode");//定义第一层菜单元素名称
for (int i = 0, len = list.getLength(); i < len; i++) {
HashMap map = new HashMap();
Node node = list.item(i);
map.put("name", node.getNodeName());//定制元素名称
NamedNodeMap nmap = node.getAttributes();
Node n = nmap.getNamedItem("ID");//定制元素属性
map.put("ID", n.getNodeValue());
NodeList supList = node.getChildNodes();
for (int k = 0, klen = supList.getLength(); k < klen; k++) {
Node supnode = supList.item(k);
if (supnode.getNodeName().equals("Caption")) {//子定制元素名称
String cap = supnode.getFirstChild().getNodeValue();
map.put("capname", cap);
}
if (supnode.getNodeName().equals("hua")) {//子定制元素属性
NamedNodeMap snmap = supnode.getAttributes();
Node sn = snmap.getNamedItem("HUA");
map.put("HAIATTR", sn.getNodeValue());
}
}
reList.add(map);
}
System.out.println("list.len: " + list.getLength());
return reList;
}
//根据第一层菜单的一个索引值获取第二层菜单的列表
public ArrayList getNameList(int index) {
ArrayList reList = new ArrayList();
NodeList list = root.getElementsByTagName("MenuNode");
if (index >= list.getLength())
return null;
list = ((Element) list.item(index)).getElementsByTagName("SubNode");
publicMethod(reList, list);
//公共代码结束
return reList;
}
/**
* @param reList
* @param list
*/
private void publicMethod(ArrayList reList, NodeList list) {
//以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
HashMap map = new HashMap();
Node node = list.item(i);
getNodeInfo(map, node);
reList.add(map);
}
}
/**
* @param i
* @param map
* @param node
*/
private void getNodeInfo(HashMap map, Node node) {
try {
if(node==null)System.out.println("node==null");
map.put("name", node.getNodeName());
NamedNodeMap nmap = node.getAttributes();
if(nmap==null)System.out.println("nmap==null");
Node n = nmap.getNamedItem("ID");
if(n==null)System.out.println("n==null");
map.put("ID", n.getNodeValue());
NodeList supList = node.getChildNodes();
for (int k = 0, klen = supList.getLength(); k < klen; k++) {
Node supnode = supList.item(k);
if (supnode.getNodeName().equals("Caption")) {
String cap = supnode.getFirstChild().getNodeValue();
map.put("capname", cap);
}
if (supnode.getNodeName().equals("hua")) {
NamedNodeMap snmap = supnode.getAttributes();
Node sn = snmap.getNamedItem("HUA");
map.put("HAIATTR", sn.getNodeValue());
}
}
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//在一个父节点对象基础上,根据子节点名称、属性得到子节点对象
public Element getNameListbyAttr(Element ele, String name,String attr) {
ArrayList reList = new ArrayList();
NodeList list = ele.getElementsByTagName(name);
//以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
//HashMap map = new HashMap();
Node node = list.item(i);
//map.put("name", node.getNodeName());
NamedNodeMap nmap = node.getAttributes();
Node n = nmap.getNamedItem("ID");
String nvalue = n.getNodeValue();
if(attr.equals(nvalue))return (Element)node;
}
//公共代码结束
return null;
}
//在一个父节点对象基础上,根据子节点名称得到子节点对象数组
public Element[] getNameAllListbyAttr(Element ele, String name) {
ArrayList reList = new ArrayList();
NodeList list = ele.getElementsByTagName(name);
Element[] eles = new Element[list.getLength()];
//以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
//HashMap map = new HashMap();
Node node = list.item(i);
eles[i] = (Element)node;
}
//公共代码结束
return eles;
}
public ArrayList getList(Element ele){
ArrayList reList = new ArrayList();
HashMap map = new HashMap();
getNodeInfo(map, ele);
reList.add(map);
return reList;
}
//根据第一、二层菜单的2个索引值获取对应第三层菜单的列表
public ArrayList getNameList(int index1, int index2) {
ArrayList reList = new ArrayList();
NodeList list = root.getElementsByTagName("MenuNode");
if (index1 >= list.getLength())
return null;
list = ((Element) list.item(index1)).getElementsByTagName("SubNode");
if (index2 >= list.getLength())
return null;
list = ((Element) list.item(index1)).getElementsByTagName("Button");
// 以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
HashMap map = new HashMap();
Node node = list.item(i);
map.put("name", node.getNodeName());
NamedNodeMap nmap = node.getAttributes();
Node n = nmap.getNamedItem("ID");
map.put("ID", n.getNodeValue());
NodeList supList = node.getChildNodes();
for (int k = 0, klen = supList.getLength(); k < klen; k++) {
Node supnode = supList.item(k);
if (supnode.getNodeName().equals("Caption")) {
String cap = supnode.getFirstChild().getNodeValue();
map.put("capname", cap);
}
if (supnode.getNodeName().equals("hua")) {
NamedNodeMap snmap = supnode.getAttributes();
Node sn = snmap.getNamedItem("HUA");
map.put("HAIATTR", sn.getNodeValue());
}
}
reList.add(map);
}
//公共代码结束
return reList;
}
public static void main(String arge[]) {
long lasting = System.currentTimeMillis();
long lasting1 = 0;
try {
YxXmlParser x = new YxXmlParser();
lasting1 = System.currentTimeMillis();
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting));
//ArrayList list = x.getNameList(0, 0);
ArrayList list = null;
Element e = x.getNameListbyAttr(YxXmlParser.root, "MenuNode", "010000");
e = x.getNameListbyAttr(e, "SubNode", "010100");
//e = x.getNameListbyAttr(e, "Button", "010102");
Element es[]= x.getNameAllListbyAttr(e, "Button");
e = es[0];
if (e == null) {
System.out.println("e指定的位置不存在!");
return;
}
list = x.getList(e);
if (list == null) {
System.out.println("list指定的位置不存在!");
return;
}
bugInfo(list);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting1)
+ "毫秒");
}
/**
* @param list
*/
private static void bugInfo(ArrayList list) {
for (int i = 0, len = list.size(); i < len; i++) {
HashMap map = (HashMap) list.get(i);
System.out.println(map);
Set set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String keystr = (String) it.next();
System.out.print(keystr + " : ");
String str = (String) map.get(keystr);
System.out.println(str);
}
}
}
}
<Menu>
<MenuNode ID="010000" dd="db">
<Caption>系统管理</Caption>
<MenuFlag>1</MenuFlag>
<SubNode ID="010100">
<Caption>职工信息</Caption>
<ID_Model>000001</ID_Model>
<Button ID="010101">
<Caption>录入</Caption>
<ID_Model>F010101</ID_Model>
</Button>
<Button ID="010102">
<Caption>修改</Caption>
<ID_Model>F010102</ID_Model>
<hua HUA="my god"></hua>
</Button>
<Parameter INDEX="1">
<TYPE>INT</TYPE>
</Parameter>
</SubNode>
</MenuNode>
</Menu>
----------------------------------------------------------------
解析类:
/*
* Created on 2005-12-15
*/
package com.yx.tool.xml;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author LHH
* xml文件解析工具类,可以根据一个元素名称和索引位置
* 返回这个元素所包含的指定名称子元素信息的一个ArrayList列表
* 子元素信息被封装一个HashMap里,HashMap可以定制以下内容:
* 该子元素名称、属性和它下一层指定名称子元素的值和属性
*/
public class YxXmlParser {
static Element root = null;
public YxXmlParser() {
try {
if (root == null) {
System.out.println("roo is builder!");
File f = new File(
"D:/YSGL/TestTag/src/com/yx/tool/xml/data_10k.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
root = doc.getDocumentElement();
}
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//获取第一层菜单的列表,别修改了,经测试是稳定的
public ArrayList getNameList() {
ArrayList reList = new ArrayList();
NodeList list = root.getElementsByTagName("MenuNode");//定义第一层菜单元素名称
for (int i = 0, len = list.getLength(); i < len; i++) {
HashMap map = new HashMap();
Node node = list.item(i);
map.put("name", node.getNodeName());//定制元素名称
NamedNodeMap nmap = node.getAttributes();
Node n = nmap.getNamedItem("ID");//定制元素属性
map.put("ID", n.getNodeValue());
NodeList supList = node.getChildNodes();
for (int k = 0, klen = supList.getLength(); k < klen; k++) {
Node supnode = supList.item(k);
if (supnode.getNodeName().equals("Caption")) {//子定制元素名称
String cap = supnode.getFirstChild().getNodeValue();
map.put("capname", cap);
}
if (supnode.getNodeName().equals("hua")) {//子定制元素属性
NamedNodeMap snmap = supnode.getAttributes();
Node sn = snmap.getNamedItem("HUA");
map.put("HAIATTR", sn.getNodeValue());
}
}
reList.add(map);
}
System.out.println("list.len: " + list.getLength());
return reList;
}
//根据第一层菜单的一个索引值获取第二层菜单的列表
public ArrayList getNameList(int index) {
ArrayList reList = new ArrayList();
NodeList list = root.getElementsByTagName("MenuNode");
if (index >= list.getLength())
return null;
list = ((Element) list.item(index)).getElementsByTagName("SubNode");
publicMethod(reList, list);
//公共代码结束
return reList;
}
/**
* @param reList
* @param list
*/
private void publicMethod(ArrayList reList, NodeList list) {
//以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
HashMap map = new HashMap();
Node node = list.item(i);
getNodeInfo(map, node);
reList.add(map);
}
}
/**
* @param i
* @param map
* @param node
*/
private void getNodeInfo(HashMap map, Node node) {
try {
if(node==null)System.out.println("node==null");
map.put("name", node.getNodeName());
NamedNodeMap nmap = node.getAttributes();
if(nmap==null)System.out.println("nmap==null");
Node n = nmap.getNamedItem("ID");
if(n==null)System.out.println("n==null");
map.put("ID", n.getNodeValue());
NodeList supList = node.getChildNodes();
for (int k = 0, klen = supList.getLength(); k < klen; k++) {
Node supnode = supList.item(k);
if (supnode.getNodeName().equals("Caption")) {
String cap = supnode.getFirstChild().getNodeValue();
map.put("capname", cap);
}
if (supnode.getNodeName().equals("hua")) {
NamedNodeMap snmap = supnode.getAttributes();
Node sn = snmap.getNamedItem("HUA");
map.put("HAIATTR", sn.getNodeValue());
}
}
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//在一个父节点对象基础上,根据子节点名称、属性得到子节点对象
public Element getNameListbyAttr(Element ele, String name,String attr) {
ArrayList reList = new ArrayList();
NodeList list = ele.getElementsByTagName(name);
//以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
//HashMap map = new HashMap();
Node node = list.item(i);
//map.put("name", node.getNodeName());
NamedNodeMap nmap = node.getAttributes();
Node n = nmap.getNamedItem("ID");
String nvalue = n.getNodeValue();
if(attr.equals(nvalue))return (Element)node;
}
//公共代码结束
return null;
}
//在一个父节点对象基础上,根据子节点名称得到子节点对象数组
public Element[] getNameAllListbyAttr(Element ele, String name) {
ArrayList reList = new ArrayList();
NodeList list = ele.getElementsByTagName(name);
Element[] eles = new Element[list.getLength()];
//以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
//HashMap map = new HashMap();
Node node = list.item(i);
eles[i] = (Element)node;
}
//公共代码结束
return eles;
}
public ArrayList getList(Element ele){
ArrayList reList = new ArrayList();
HashMap map = new HashMap();
getNodeInfo(map, ele);
reList.add(map);
return reList;
}
//根据第一、二层菜单的2个索引值获取对应第三层菜单的列表
public ArrayList getNameList(int index1, int index2) {
ArrayList reList = new ArrayList();
NodeList list = root.getElementsByTagName("MenuNode");
if (index1 >= list.getLength())
return null;
list = ((Element) list.item(index1)).getElementsByTagName("SubNode");
if (index2 >= list.getLength())
return null;
list = ((Element) list.item(index1)).getElementsByTagName("Button");
// 以下是公共代码
for (int i = 0, len = list.getLength(); i < len; i++) {
HashMap map = new HashMap();
Node node = list.item(i);
map.put("name", node.getNodeName());
NamedNodeMap nmap = node.getAttributes();
Node n = nmap.getNamedItem("ID");
map.put("ID", n.getNodeValue());
NodeList supList = node.getChildNodes();
for (int k = 0, klen = supList.getLength(); k < klen; k++) {
Node supnode = supList.item(k);
if (supnode.getNodeName().equals("Caption")) {
String cap = supnode.getFirstChild().getNodeValue();
map.put("capname", cap);
}
if (supnode.getNodeName().equals("hua")) {
NamedNodeMap snmap = supnode.getAttributes();
Node sn = snmap.getNamedItem("HUA");
map.put("HAIATTR", sn.getNodeValue());
}
}
reList.add(map);
}
//公共代码结束
return reList;
}
public static void main(String arge[]) {
long lasting = System.currentTimeMillis();
long lasting1 = 0;
try {
YxXmlParser x = new YxXmlParser();
lasting1 = System.currentTimeMillis();
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting));
//ArrayList list = x.getNameList(0, 0);
ArrayList list = null;
Element e = x.getNameListbyAttr(YxXmlParser.root, "MenuNode", "010000");
e = x.getNameListbyAttr(e, "SubNode", "010100");
//e = x.getNameListbyAttr(e, "Button", "010102");
Element es[]= x.getNameAllListbyAttr(e, "Button");
e = es[0];
if (e == null) {
System.out.println("e指定的位置不存在!");
return;
}
list = x.getList(e);
if (list == null) {
System.out.println("list指定的位置不存在!");
return;
}
bugInfo(list);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting1)
+ "毫秒");
}
/**
* @param list
*/
private static void bugInfo(ArrayList list) {
for (int i = 0, len = list.size(); i < len; i++) {
HashMap map = (HashMap) list.get(i);
System.out.println(map);
Set set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String keystr = (String) it.next();
System.out.print(keystr + " : ");
String str = (String) map.get(keystr);
System.out.println(str);
}
}
}
}
相关文章推荐
- 事件代理总结: 已经有一些使用主流类库的事件代理示例出现了,比如说jQuery、Prototype以及Yahoo! UI。你也可以找到那些不用任何类库的例子,比如说Usable Type blog上的这一个。一旦需要的话,事件代理将是你工具箱里的一件得心应手的工具,而且它很容易实现。
- Ansible@一个有效的配置管理工具--Ansible configure management--翻译(十)
- Ansible :一个配置管理和IT自动化工具
- 发布一个JavaScript工具类库jutil,欢迎使用,欢迎补充,欢迎挑错!
- 手把手叫你 制作一个不需要任何工具 从网页上安装ipa包的办法
- 找回win8开始菜单,不用任何工具
- 一个非常实用的Android开源工具类库
- 手把手叫你 制作一个不需要任何工具 从网页上安装ipa包的办法
- 一个WinForm程序配置信息的简单模型和维护工具——使用说明
- C-不用任何库函数,写一个内存拷贝函数
- [cnblogs镜像]macOS 不用任何第三方工具 简单两步使用 Automator 将截图转成@1x
- 蛙蛙推荐:自己做一个抓包工具(类库是用的别人的)
- 一个js文件加解密工具,可以得到不开源的源码
- SQO2008配置管理工具服务显示远程过程调用失败 前两天,装了VS2012后,打开SQL2008配置管理工具,发现SQL服务名称里什么也没有,只有一个提示:(如图)
- 《企业考试系统》项目实践(04):通用工具类库之配置文件操作工具
- 用dos命令扫描一个网段的全部端口,不需要任何工具
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)
- 两个网卡只能有一个网关连接内网的网卡不用配置网关
- 作为一名及格的站长都知道内容为王,外链为皇”,一个上网站想要有一个好的排名除了要有吸惹人高质量的内容外还需要强年夜的外链。对于网站内容要环绕网站主题,若是自己文采欠好可以伪原创下,万万不要用工具伪原创