xml解析类,不用任何工具类库,一个类全搞掂你想要的(一)

李海华 2005-12-21 11:37:18
/*
* 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();
}
}
...全文
240 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fbtdjs 2005-12-26
  • 打赏
  • 举报
回复
收藏
李海华 2005-12-21
  • 打赏
  • 举报
回复
dng

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧