您的位置:首页 > 其它

用DOM/JDOM解析XML文件

2006-04-14 14:42 417 查看
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;

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();
}
}

/**
* <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
*/

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的容错性、可扩展性似乎更好些。这一页的内容太多,还是放到下一页吧……

看到了吧,那么复杂的解析,还是Jdom来得快了。

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);
}
}
// 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****************************");
// }
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;
}
/**
* 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[] 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 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 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();
}
/**
* Deletes the specified property.
*
* @param name the property to delete.
*/ 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();
}
/**
* Update the value of the specified property.
* @param name
* @param value
*/ 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();
}
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: