 |
建站必读 |
 |
|
|
 |
|
 |
|
| |
| 当前位置:首页 -> 建站必读 -> JSP技术 |
|
搜集整理的对xml文件操作的java程序,基本可以满足基本的操作。 |
包括生成xml文件,增加删除修改节点,增加修改属性等等。
现把我的程序共享,还有我的测试例子,看看大家有什么更好的建议。
其中用到了jdom.jar 和xerces-1.2.2.jar ,都是标准的xml解析包。
可以从网上下载。
/**
* $RCSfile: XMLProperty.java,v $
* $Revision: 1.3 $
* $Date: 2002/04/02 21:17:09 $
*
* Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package crm.bean.common;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.xml.sax.*;
/**
* Provides the the ability to use simple XML property files. Each property is
* in the form X.Y.Z, which would map to an XML snippet of:
* <pre>
* <X>
* <Y>
* <Z>someValue</Z>
* </Y>
* </X>
* </pre>
*
* The XML file is passed in to the constructor and must be readable and
* writtable. Setting property values will automatically persist those value
* to disk.
*/
public class XMLProperty {
private String xfile = null;
private File file;
private Document doc;
private String PGID="XMLProperty.java";
/**
* Parsing the XML file every time we need a property is slow. Therefore,
* we use a Map to cache property values that are accessed more than once.
*/
private Map propertyCache = new HashMap();
/**
* Creates a new XMLProperty object.
*
* @param file the full path the file that properties should be read from
* and written to.
*/
public XMLProperty(String file)
{
this.file = new File(file);
this.xfile = file;
try
{
SAXBuilder builder = new SAXBuilder();
doc = builder.build(new File(file));
}
catch (Exception e)
{
System.err.println("Error creating XML parser in " + PGID);
e.printStackTrace();
}
//
}
public XMLProperty(Document doc)
{
this.doc=doc;
}
public Document getDocument()
{
return this.doc;
}
/**
* 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, giv |
| |
|
| |
本站关键词: |
|
|
|
|
 |
|
 |
|