王朝网络
分享
 
 
 

用DOM/JDOM解析XML文件

王朝java/jsp·作者佚名  2006-02-04
宽屏版  字体: |||超大  

夜已经深了,可是我仍未眠。明天就要回家过年了,对于一个飘荡在外的游子来说,回家过年是一件多么激动人心的事啊!这才是我心中真正的年啊!

真希望新的一年快点到来,因为有太多的不愉快需要忘记、因为有太多的梦想需要去实现……

前一段时间要解析一个XML文件,就是对XML进行增、删、改的操作。以前做J2EE时一直都是用第三方的类库来辅助解决的,但这次却要求自己用DOM来解析,只能用JDK自带的类库!

一直到现在还没有想通:为什么要重复发明轮子呢???但是事情还是要做的,我只是俗人,我也要吃饭……

又或许,这只是我的又一个自我解嘲的借口罢了!

下边的类其实有很多不足的地方的,但我却始终不能说服我自己, 我就是这样一个倔强又刚愎自用的人,期待来年吧,希望走过本命年的我能成熟起来,对于一些事情的控制更自由一些。。。

import java.io.File;

import java.io.FileOutputStream;

import java.util.ArrayList;

import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

/**

* <p>ConfigParser</p>

* 1. read the config XML,or update the file.</br>

* 2. query a property from the config file,the property name fromat:lss.agent.version etc.</br>

* 3. add properties into the config file.</br>

*

* <p>@author javer QQ:84831612</p>

*/

public class ConfigParser

{

private File xmlFile = null;

private Document doc = null;

private Element root = null;

public ConfigParser(String filename)

{

xmlFile = new File(filename);

try

{

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

doc = builder.parse(xmlFile);

root = doc.getDocumentElement();

}

catch(Exception e)

{

System.err.println("Error creating XML parser in ConfigParser.java");

e.printStackTrace();

}

}

/**

* Return the value of the specified property.

*

* @param name the name of the property to get.

* @return the value of the specified property.

*/

public String getValue(String name)

{

String[] propName = parsePropertyName(name);

NodeList nodes = root.getElementsByTagName(propName[0]);

if(nodes == null || nodes.getLength() < 1)

return null;

for(int i = 1; i < propName.length; i++)

{

nodes = ((Element)nodes.item(0)).getElementsByTagName(propName[i]);

if(nodes == null || nodes.getLength() < 1)

return null;

}

Node node = nodes.item(0).getChildNodes().item(0);

if(node != null)

return node.getNodeValue();

else

return null;

}

/**

* <p>Add the values by the specified properties.</p>

* For example:</br>

* if values is existed as follow</br>

* x.y.z1=1</br>

* .z2=2</br>

* .z3=3</br>

* and now, you need add new values as a new group,like as follow values</br>

* x.y.z1=11</br>

* .z2=22</br>

* .z3=33</br>

* you need call this function like as follow:</br>

* addValue("x.y",new String[]{"z1","z2","z3"},new String[]{"11","22","33"})</br>

* and the XML will have a result as follow:</br>

* <xml>

* <x>

* <y>

* <z1>1</z1>

* <z2>2</z2>

* <z2>3</z2>

* </y>

* <y>

* <z1>11</z1>

* <z2>22</z2>

* <z2>33</z2>

* </y>

* </x>

* </xml>

*

* @param parentName

* @param subNames

* @param values

*/

public void addValue(String parentName, String[] subNames, String[] values)

{

String[] propName = parsePropertyName(parentName);

NodeList nodes = root.getElementsByTagName(propName[0]);

if(nodes == null || nodes.getLength() < 1)

{

root.appendChild(doc.createElement(propName[0]));

nodes = root.getElementsByTagName(propName[0]);

}

Element tempElement = ((Element)nodes.item(0));

for(int i = 1; i < propName.length - 1; i++)

{

tempElement = ((Element)nodes.item(0));

nodes = tempElement.getElementsByTagName(propName[i]);

if(nodes == null || nodes.getLength() < 1)

{

tempElement.appendChild(doc.createElement(propName[i]));

nodes = tempElement.getElementsByTagName(propName[i]);

}

}

tempElement = (Element)tempElement.appendChild(doc.createElement(propName[propName.length - 1]));

Node node = null;

for(int i = 0; i < subNames.length; i++)

{

node = tempElement.appendChild(doc.createElement(subNames[i]));

node.appendChild(doc.createTextNode(values[i]));

}

store();

}

/**

* Update the value of the specified property.

* @param name

* @param value

*/

public void updateValue(String name, String value)

{

String[] propName = parsePropertyName(name);

NodeList nodes = root.getElementsByTagName(propName[0]);

if(nodes == null || nodes.getLength() < 1)

return;

for(int i = 1; i < propName.length; i++)

{

nodes = ((Element)nodes.item(0)).getElementsByTagName(propName[i]);

if(nodes == null || nodes.getLength() < 1)

return;

}

Node node = nodes.item(0).getChildNodes().item(0);

if(node != null)

node.setNodeValue(value);

else

return;

store();

}

/**

* Deletes the specified property.

*

* @param name the property to delete.

*/

public void deleteProperty(String name)

{

String[] propName = parsePropertyName(name);

if(propName.length == 1)

root.removeChild(root.getElementsByTagName(propName[0]).item(0));

else

{

NodeList nodes = root.getElementsByTagName(propName[0]);

for(int i = 1; i < propName.length - 1; i++)

{

nodes = ((Element)nodes.item(0)).getElementsByTagName(propName[i]);

if(nodes == null || nodes.getLength() < 1)

return;

}

nodes.item(0).removeChild(((Element)nodes.item(0)).getElementsByTagName(

propName[propName.length - 1]).item(0));

}

store();

}

/**

* Return all children property names of a parent property as a String array,

* or an empty array if the if there are no children. For example, given

* the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>, then

* the child properties of <tt>X.Y</tt> are <tt>A</tt>, <tt>B</tt>, and

* <tt>C</tt>.

*

* @param parent the name of the parent property.

* @return all child property values for the given parent.

*/

public String[] getChildrenProperties(String parent)

{

String[] propName = parsePropertyName(parent);

NodeList nodes = root.getElementsByTagName(propName[0]);

if(nodes == null || nodes.getLength() < 1)

return null;

for(int i = 1; i < propName.length; i++)

{

nodes = ((Element)nodes.item(0)).getElementsByTagName(propName[i]);

if(nodes == null || nodes.getLength() < 1)

return null;

}

nodes = nodes.item(0).getChildNodes();

ArrayList list = new ArrayList();

for(int i = 0; i < nodes.getLength(); i++)

{

if(nodes.item(i).getNodeType() == Node.ELEMENT_NODE)

list.add(nodes.item(i).getNodeName());

}

if(list == null)

return null;

else

return (String[])list.toArray(new String[list.size()]);

}

/**

* Return the value when the specified property key and value is existent,

* and return null if the specified property key and value is not existent

*

* @param name

* @param value

* @return

*/

public String findValue(String name, String value)

{

String[] propName = parsePropertyName(name);

return findValue(root, 0, propName, value);

}

private String findValue(Element element, int depth, String[] propName,String value)

{

String _value = null;

if(depth == propName.length)

{

_value = element.getChildNodes().item(0).getNodeValue();

if(_value.equals(value))

return value;

else

return null;

}

NodeList children = element.getElementsByTagName(propName[depth]);

if(children == null || children.getLength() < 1)

return null;

else

++depth;

for(int i = 0; i < children.getLength(); i++)

{

if(children.item(i).getNodeType() != Node.ELEMENT_NODE)

continue;

element = (Element)children.item(i);

_value = findValue(element, depth, propName, value);

if(_value != null)

{

return value;

}

}

return null;

}

// public static void main(String[] args) throws Exception

// {

// System.out.println("Start****************************");

// ConfigParser test = new

// ConfigParser("/test.xml");

// update

// System.out.println(test.getValue("x.y"));

// test.updateValue("x.y","xyz");

// System.out.println(test.getValue("x.y"));

// delete

// System.out.println(test.getValue("x.y"));

// test.deleteProperty("x.y");

// System.out.println(test.getValue("x.y"));

// getChildrenProperties

// String[] childrenNames = test.getChildrenProperties("x.y");

// if(childrenNames!=null)

// for(int i=0;i<childrenNames.length;i++)

// {

// System.out.println("childrenNames["+i+"]=" + childrenNames[i]);

// }

// find

// System.out.println(test.findValue("x.y.z1","cc22"));

// add

// test.addValue("x.y",new String[]{"z1","z2","z3","z4"},new String[]{"11","22","33","44"}

// );

//

// System.out.println("End****************************");

// }

private synchronized void store()

{

boolean error = false;

File tempFile = new File(xmlFile.getParentFile(), xmlFile.getName() + ".tmp");

FileOutputStream out = null;

try

{

DOMSource ds = new DOMSource(root);

out = new FileOutputStream(tempFile);

StreamResult sr = new StreamResult(out);

TransformerFactory tff = TransformerFactory.newInstance();

Transformer tf = tff.newTransformer();

tf.transform(ds, sr);

}

catch(Exception e)

{

e.printStackTrace();

error = true;

}

finally

{

try

{

out.flush();

out.close();

}

catch(Exception e)

{

e.printStackTrace();

error = true;

}

}

if(!error)

{

xmlFile.delete();

tempFile.renameTo(xmlFile);

}

}

private String[] parsePropertyName(String name)

{

int size = 1;

for(int i = 0; i < name.length(); i++)

{

if(name.charAt(i) == '.')

{

size++;

}

}

String[] propName = new String[size];

StringTokenizer tokenizer = new StringTokenizer(name, ".");

int i = 0;

while(tokenizer.hasMoreTokens())

{

propName[i] = tokenizer.nextToken();

i++;

}

return propName;

}

}

既然提到了JDOM,就不得不搞清楚它和DOM的关系,免得被人问及为何选择此种解决方案时再次无言 ;) (具体的理论性质的比较,google、baidu上有一堆堆的说明,这里就不再copy)同样的类,还有一个用JDOM的实现版本,同样多的代码量下,JDOM的容错性、可扩展性似乎更好些。这一页的内容太多,还是放到下一页吧……

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有