您的位置:首页 > 编程语言 > Java开发

xml文件在JAVA读取过程中的解析

2015-11-07 20:27 633 查看
   xml文件在应用过程中由于配置的优越性被广大程序员广泛试用,我在项目中遇到了关于它的读取过程的问题,经过一段时间的查找找到了解决的办法,简单的说一下吧。

我采用的jar包是dom4j,dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。下面对dom4j比较重要的api进行一下讲解。

一.DOM4j中,获得Document对象的方式有三种:

1.读取XML文件,获得document对象
SAXReader reader = new SAXReader();
Document   document = reader.read(new File("csdn.xml"));
2.解析XML形式的文本,得到document对象.
String text = "<csdn></csdn>";
Document document = DocumentHelper.parseText(text);
3.主动创建document对象.
Document document = DocumentHelper.createDocument();             //创建根节点
Element root = document.addElement("csdn");


二.节点对象操作的方法

1.获取文档的根节点.
Element root = document.getRootElement();
2.取得某个节点的子节点.
Element element=node.element(“四大名著");
3.取得节点的文字
String text=node.getText();
4.取得某节点下所有名为“csdn”的子节点,并进行遍历.
List nodes = rootElm.elements("csdn");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
5.对某节点下的所有子节点进行遍历.
for(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element) it.next();
// do something
}
6.在某节点下添加子节点
Element elm = newElm.addElement("朝代");
7.设置节点文字.  elm.setText("明朝");
8.删除某节点.//childElement是待删除的节点,parentElement是其父节点  parentElement.remove(childElment);
9.添加一个CDATA节点.Element contentElm = infoElm.addElement("content");contentElm.addCDATA(“cdata区域”);

三、节点对象的属性方法操作

1.取得某节点下的某属性    Element root=document.getRootElement();        //属性名name
Attribute attribute=root.attribute("id");
2.取得属性的文字
String text=attribute.getText();
3.删除某属性 Attribute attribute=root.attribute("size"); root.remove(attribute);
4.遍历某节点的所有属性
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
5.设置某节点的属性和文字.   newMemberElm.addAttribute("name", "sitinspring");
6.设置属性的文字   Attribute attribute=root.attribute("name");   attribute.setText("csdn");

在我自己的项目中,需求是我的JAVA程序要能够从xml文件中配置数据库信息,SQL语句以及对应的操作说明,我所用的xml文件如下。

<CONFIG>
<a>

</a>

<b>

</b>

<c>

</c>

<select-items>
<command id="SALE0102" config="mitconfig">
SELECT * from hello;
</command>

<command id="nilaisa" config="pitconfig">
select * from hha;
</command>

<command id="henhen" config="jitconfig">
select * from hhah;
</command>

</select-items>

<misconfig>
<server>166.100.0.13</server>
<dbname>mistest</dbname>
<user>mistest</user>
<password>mistest</password>
<port>1521</port>
</misconfig>

</CONFIG>

我新建了一个读取xml的文件,然后对xml进行解析操作。

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLReader {
// 配置文件名
private static String filename = "Settings.xml";
private static Vector<String> idVector = new Vector<String>();
private static Vector<String> configVector = new Vector<String>();
private static Vector<String> sqlVector = new Vector<String>();

public static Vector<String> getListData(){
//		Vector<String> listVector = new Vector<String>();
try {
File f = new File(filename);
if (!f.exists()) {
System.out.println("  Error : Config file doesn't exist!");
System.exit(1);
}
SAXReader reader = new SAXReader();
Document doc;
doc = reader.read(f);
Element root = doc.getRootElement();
List<Element> listElement=root.elements();
for (int i = 0; i < listElement.size(); i++) {
if (listElement.get(i).getName().equals("select-items")) {
List<Element> listson = listElement.get(i).elements();
for (int j = 0; j < listson.size(); j++) {
String name = listson.get(j).getName();
String sql = listson.get(j).getTextTrim();
sqlVector.addElement(sql);
final List<Attribute> listAttr = listson.get(j).attributes();// 当前节点的所有属性
for (final Attribute attr : listAttr) {// 遍历当前节点的所有属性
final String shuxing = attr.getName();// 属性名称
final String value = attr.getValue();// 属性的值
if (shuxing.equals("id")) {
idVector.add(value);
}else {
configVector.add(value);
}
}
}

}
}

} catch (DocumentException e) {
e.printStackTrace();
}
return idVector;
}

public static Vector<String> getSqlVector(){
for (int i = 0; i < sqlVector.size(); i++) {
System.out.println(sqlVector.get(i));
}
return sqlVector;
}

public static Vector<String> getConfigVector(){
for (int i = 0; i < configVector.size(); i++) {
System.out.println(configVector.get(i));
}
return configVector;
}

public static Config getConfig(String configName){
Config Config = new Config();
try {
File f = new File(filename);
if (!f.exists()) {
System.out.println("  Error : Config file doesn't exist!");
System.exit(1);
}
SAXReader reader = new SAXReader();
Document doc;
doc = reader.read(f);
Element root = doc.getRootElement();
Element data;
Iterator<?> itr_pos = root.elementIterator(configName);
data = (Element) itr_pos.next();

Config.server = data.elementText("server").trim();
Config.user = data.elementText("user").trim();
Config.password = data.elementText("password").trim();
Config.
947f
port = data.elementText("port").trim();
Config.dbname = data.elementText("dbname").trim();

} catch (Exception ex) {
System.out.println("Error : " + ex.toString());
}
return Config;
}

}

通过对XML文件的遍历,从而找出所需的信息。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: