DOM解析xml


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;



public class OneZoreFiveCFGParse {

/*************************************************
*一下的List为全局List,用来给外部调用提供数据接口
*
***************************************************/
private List<String> realClassList = new ArrayList<String>();//存放根节点元素名称
private List<String> classNameList = new ArrayList<String>();// 存放类信息中name:集合
private List<String> fieldNameList = new ArrayList<String>();// 存放类信息中field:集合
private List<List<String>> elemList = new ArrayList<List<String>>();// 存放类信息中field:集合

// private static final String FILE_CFG_PATH = "C:\\xml\\105.cfg";// 要装换的cfg文件路径
private Document document = null;

public OneZoreFiveCFGParse(String path) {
createDocument(path);// 创建文档对象
parseCFG();// 解析xml文件,并且将解析的数据封装在容器中,实现数据初始化
}

// 创建文档对象
private void createDocument(String path) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File (path);
if(!file.exists()){
return;
}
document = builder.parse(file);
} catch (Exception e) {
e.printStackTrace();
}
}

// 解析CFG文件,并将数据进行封装
private void parseCFG() {
// 获取根节点元素
Element rootElement = document.getDocumentElement();
//根节点元素名称
String rootNodeName = rootElement.getNodeName();
rootNodeName = rootNodeName.substring(rootNodeName.indexOf(":")+1);
realClassList.add(rootNodeName);
// 获取不同形式的配置信息集合
NodeList configurationNodeList = rootElement.getElementsByTagName("configurations");
if (configurationNodeList == null || configurationNodeList.getLength() == 0) {
return;
}
for (int i = 0; i < configurationNodeList.getLength(); i++) {
// 获取类信息
Element configurationElement = (Element) configurationNodeList.item(i);
// 获取configurations下的paras集合(存放类信息)
NodeList paraNodeList = configurationElement.getElementsByTagName("paras");
if (paraNodeList == null || paraNodeList.getLength() == 0) {
return;
}
for (int j = 0; j < paraNodeList.getLength(); j++) {
//每个paras创建一个List
List<String> parasList = new ArrayList<String>();
Element paraElement = (Element) paraNodeList.item(j);// 获取一个para(类)信息
Attr paraAttr = paraElement.getAttributeNode("name");// 属性
if(paraAttr == null){//为空验证,如果存在空paras就跳过
continue;
}else{
String clazzName = paraAttr.getValue();// 获取类名称
classNameList.add(clazzName);
}
// RUANQIANG 获取类中的默认数据,先不管
/*************************************************************************************************/
NodeList elemsNodeList = paraElement.getElementsByTagName("elems");
if (elemsNodeList != null || elemsNodeList.getLength() > 0) {//没有默认值
for(int h=0;h<elemsNodeList.getLength();h++){
StringBuffer stringBuffer = new StringBuffer();//存放field值,每个
Element elemElment =(Element)elemsNodeList.item(h);
NodeList fieldsNodeList = elemElment.getElementsByTagName("fields");
//在每一个elems下定义生命一个StringBuffer存放fields的属性和值
for(int o=0;o<fieldsNodeList.getLength();o++){
//RUANQIANG 可能要判断,如果值为空是否抓取其值
Element fiedsElement =(Element)fieldsNodeList.item(o);
Attr nameAttr = fiedsElement.getAttributeNode("name");//获取变量名
Attr valueAttr = fiedsElement.getAttributeNode("value");//获取变量对应的值
//给变量赋值
if(nameAttr != null && valueAttr!=null){
stringBuffer.append(nameAttr.getNodeValue()+":="+valueAttr.getNodeValue());
}else if(nameAttr != null){
if(valueAttr==null){
stringBuffer.append(nameAttr.getNodeValue()+":="+"");
}
}
stringBuffer.append(";");
}
parasList.add(stringBuffer.toString());
}
}
elemList.add(parasList);
/***********************************************************************************************/
// 获取类中所有全局变量(类的属性)
NodeList fieldNamesNodeList = paraElement.getElementsByTagName("fieldNames");
if (fieldNamesNodeList == null || fieldNamesNodeList.getLength() == 0) {
return;
}
// RUANQIANG 很关键定义一个StringBuffer将所有的属性名放进去
StringBuffer stringBuffer = new StringBuffer();
for (int k = 0; k < fieldNamesNodeList.getLength(); k++) {
stringBuffer.append(",");
Element fieldElement = (Element) fieldNamesNodeList.item(k);
Attr fieldAttr = fieldElement.getAttributeNode("name");// 属性
String fieldName = fieldAttr.getValue();// 获取类属性
stringBuffer.append(fieldName);
}
fieldNameList.add(stringBuffer.toString());
}
}
}





public List<String> getRealClassList() {
return realClassList;
}

public void setRealClassList(List<String> realClassList) {
this.realClassList = realClassList;
}

public List<String> getClassNameList() {
return classNameList;
}

public void setClassNameList(List<String> classNameList) {
this.classNameList = classNameList;
}

public List<String> getFieldNameList() {
return fieldNameList;
}

public void setFieldNameList(List<String> fieldNameList) {
this.fieldNameList = fieldNameList;
}

public List<List<String>> getElemList() {
return elemList;
}

public void setElemList(List<List<String>> elemList) {
this.elemList = elemList;
}

}

你可能感兴趣的:(dom解析xml)