一下是项目中的类文件例子。
package com.hbboao.votaglib.xml;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
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 abstract class XMLDataBean
{
private String filename = "";
private Document dom = null;
public XMLDataBean()
{
}
public Document getDom()
{
return dom;
}
public void setDom(Document dom)
{
this.dom = dom;
}
public String getFilename()
{
return filename;
}
public void setFilename(String filename)
{
this.filename = filename;
}
public abstract void ParseElementNode(Element node, Object parent);
public abstract void ParseAttributeNode(Attr node, Object parent);
public void ParseFromNode(Node node, Object parent)
{
int checkNode;
checkNode = node.getNodeType();//判断node类型
switch (checkNode)
{
case Node.ATTRIBUTE_NODE:
{
ParseAttributeNode((Attr) node, parent);
break;
}
case Node.ELEMENT_NODE://如果是element类型的节点则执行方法ParseElementNode() 在子类中实现
{
System.out.println("执行元素方法-------");
ParseElementNode((Element) node, parent);
}
}
}
public void ParseChildNodes(Node node, Object parent)
{
NodeList list = node.getChildNodes();//得到字节点
for (int i = 0; i < list.getLength(); i++)//依次遍历所有字节点
{
ParseFromNode(list.item(i), parent);
}
}
// 声明了以后可以调用已经声明此异常的函数
public void ParseFromXml()
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
//定义工厂 API,使应用程序能够从 XML 文档获取生成 DOM 对象树的解析器。
InputStream in = null;
try
{
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
/*定义 API, 使其从 XML 文档获取 DOM 文档实例。使用此类,应用程序员可以从
XML 获取一个 Document。
此类的实例可以从 DocumentBuilderFactory.newDocumentBuilder() 方法获取。
获取此类的实例之后,将可以从各种输入源解析 XML。这些输入源为 InputStreams、
Files、URL 和 SAX InputSources。
注意,此类重用了 SAX API 中的一些类。这并不要求底层 DOM 实现的实现者使用
SAX 解析器将 XML 文档解析为 Document。它仅要求该实现使用这些现有的 API 与应用程序交流。
*/
in = new FileInputStream(filename);//将名为filename的文件读入流
dom = docBuilder.parse(in);//parse(InputStream is)
//将给定 InputStream 的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象。
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
System.out.println("从第一个字节点开始::"+dom.getFirstChild().toString());
ParseFromNode(dom.getFirstChild(), null);
}
}
package com.hbboao.votaglib.cache;
import java.util.TreeMap;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import com.hbboao.votaglib.xml.XMLDataBean;
public class DatadicCache extends BaseCache
{
private class DataDicheModle extends XMLDataBean
{
public void ParseElementNode(Element node, Object parent)
{
// TODO Auto-generated method stub
// 子类和父类的联系
System.out.println("开始执行。。。。。。。。。。");
String tag;
tag = node.getTagName();//
System.out.println("得到标签名字tagname=::::"+tag);
if (tag.equals("datadic"))//没有属性的节点直接向下查找其字节点
{
ParseChildNodes(node, parent);
}
else if (tag.equals("section"))//没有属性的节点直接向下查找其字节点
{
ParseChildNodes(node, parent);
}
else if (tag.equals("attr"))
{
TreeMap treeMap = new TreeMap();//建立一个树 用来放置数据
String keyName = node.getAttribute("code");
System.out.println("取得attr节点的code名称:::"+keyName);
map.put(keyName, treeMap);
ParseChildNodes(node, treeMap);
}
else if (tag.equals("item"))//叶子节点取得整体树形结构
{
String code = node.getAttribute("code");
String name = node.getAttribute("name");
System.out.println("取得item节点的code属性:::"+code);
System.out.println("取得item节点的name属性:::"+name);
((TreeMap) parent).put(code, name);
}
// 每个节点都循环执行子节点[抓主共性]
}
public void ParseAttributeNode(Attr node, Object parent)
{
// TODO Auto-generated method stub
}
}
public void loadFromFile()
{
System.out.println("开始解析" + super.getFileName());
DataDicheModle modle = new DataDicheModle();
modle.setFilename(getLocation() + super.getFileName());//取得完整的文件名称
System.out.println("@@@@@@@@@modle.getFileName=:::"+modle.getFilename());
modle.ParseFromXml();
System.out.println("解析完毕" + super.getFileName());
}
}
package com.hbboao.votaglib.cache;
import java.net.URL;
import java.util.Map;
import java.util.TreeMap;
public class BaseCache
{
public static Map map = new TreeMap();
private String filePath = "";
private String fileName = "";
public String getLocation()
{
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
filePath = url.toString();//取得调用类所在绝对地址 格式为 类型:地址
System.out.println("@@@@@@filePath1=::::"+filePath);
filePath = filePath.replaceFirst("jar", "file");// 有两种可能,jar或file
System.out.println("@@@@@@filePath2=::::"+filePath);
filePath = filePath.substring(5);//取得绝对路径
System.out.println("@@@@@@filePath3=::::"+filePath);
int index = filePath.lastIndexOf("classes");//得到字符串是"classes"的位置数
filePath = filePath.substring(1, index);//截取到“classes”的父目录
System.out.println("@@@@@@filePath4=::::"+filePath);
return filePath;
}
public static Map getMap()
{
return map;
}
public void setMap(Map map)
{
this.map = map;
}
public String getFilePath()
{
return filePath;
}
public void setFilePath(String filePath)
{
this.filePath = filePath;
}
public String getFileName()
{
return fileName;
}
public void setFileName(String fileName)
{
this.fileName = fileName;
}
}
上面三个类中,BaseCache主要提供了找到文件目录的功能,然后在DatadicCache类中,实现了XMLDataBean中解析XML文件的虚方法,循环调用,遍历文件,放到TreeMap中。XMLDataBean主要用于得到一个指向特定文件的Dom对象。通过以上三个类的配合,完成XML的解析。当然,在解析的过程中,具体的标签名称的判断还要根据你自己生成的XML文件的结构来定。