六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

搜集整理的對(duì)xml文件設(shè)置的java程序,基本可以滿足基本的設(shè)置。

[摘要]包括生成xml文件,增加刪除修改節(jié)點(diǎn),增加修改屬性等等。現(xiàn)把我的程序共享,還有我的測(cè)試?yán)樱纯创蠹矣惺裁锤玫慕ㄗh。其中用到了jdom.jar 和xerces-1.2.2.jar ,都是標(biāo)準(zhǔn)的xm...
包括生成xml文件,增加刪除修改節(jié)點(diǎn),增加修改屬性等等。
現(xiàn)把我的程序共享,還有我的測(cè)試?yán)樱纯创蠹矣惺裁锤玫慕ㄗh。
其中用到了jdom.jar 和xerces-1.2.2.jar ,都是標(biāo)準(zhǔn)的xml解析包。
可以從網(wǎng)上下載。
/**
* $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, 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);
// Search for this property by traversing down the XML heirarchy.
Element element = doc.getRootElement();
for (int i = 0; i < propName.length; i++) {
element = element.getChild(propName[i]);
if (element == null) {
// This node doesn't match this part of the property name which
// indicates this property doesn't exist so return empty array.
return new String [] { };
}
}
// We found matching property, return names of children.
List children = element.getChildren();
int childCount = children.size();
String [] childrenNames = new String[childCount];
for (int i=0; i<childCount; i++) {
childrenNames[i] = ((Element)children.get(i)).getName();
}
return childrenNames;
}

//qiuss 2001.11.08
public List getChildren(String name)
{

String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy.
Element element = doc.getRootElement();

if(propName!=null)
{// qiuss
for (int i = 0; i < propName.length; i++)
{
element = element.getChild(propName[i]);
if (element == null)
{
// This node doesn't match this part of the property name which
// indicates this property doesn't exist so return null.
return null;
}
}
}
// We found matching property, return names of children.
return(element.getChildren());
}
/**
*@param element element==null start from root
*@param namename==null return this element's value
*@author qiuss
*@date 2001.11.08
*/
public String getProperty(Element element,String name)
{
if (propertyCache.containsKey(name))
{
return (String)propertyCache.get(name);
}
String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy.
if(element==null) //from rootElement when element equals null
element = doc.getRootElement();
if(propName!=null)
{
for (int i = 0; i < propName.length; i++)
{
element = element.getChild(propName[i]);
if (element == null)
{
// This node doesn't match this part of the property name which
// indicates this property doesn't exist so return null.
return null;
}
}
}
// At this point, we found a matching property, so return its value.
// Empty strings are returned as null.
String value = element.getText();
if ("".equals(value)) {
return null;
}
else {
value = value.trim();
propertyCache.put(name, value);
return value;
}
}
/**
* Returns the value of the specified property.
* @param name the name of the property to get.
* @return the value of the specified property.
*/
public String getProperty(String name)
{
return getProperty(null,name);
}
/**
*@param element element==null start from root
*@param namechild's element name eg. X.Y.Z
*@param attName attribute name eg. "abc=1234" if attName==null get first element
*@return searched element
*@author qiuss
*@date 2001.11.08
*/
public Element getChild(Element element,String name,String attName)
{
//if(name==null) return element;
String[] propName = parsePropertyName(name);
String[] attrName = parseAttributeName(attName);
 
// Search for this property by traversing down the XML heirarchy.
if(element==null)
element = doc.getRootElement();
if(propName!=null)
{
// qiuss
for (int i = 0; i < propName.length; i++)
{
element = element.getChild(propName[i]);
if (element == null)
{
// This node doesn't match this part of the property name which
// indicates this property doesn't exist so return null.
return null;
}
}
}
// We found matching property, return names of children.
List eList=element.getChildren();
if(attrName==null)
return (Element)eList.get(0);
Iterator it=eList.iterator();
while(it.hasNext()){
Attribute att;
Element ele=(Element)it.next();
if((att=ele.getAttribute(attrName[0]))!=null)
{
if(att.getValue().equals(attrName[1]))
return ele;
}
}
return null;
}
/**
 * 設(shè)置一個(gè)元素值。如果指定的元素不存在,就自動(dòng)創(chuàng)建該元素。
 *
 * @param name 需要賦值的元素名稱,格式為 X.Y.Z
 * @param value 元素值
 *
 * @throws IOException
 */
public void setProperty(String name, String value)
{
propertyCache.put(name, value);

//查找指定的元素元素
Element element = this.findCreate(name);
element.setText(value);
saveProperties();
}

/**
 * 設(shè)置一個(gè)元素的Attribute值。如果指定的元素不存在,就自動(dòng)創(chuàng)建該元素。
 *
 * @param name 需要賦值的元素名稱,格式為 X.Y.Z
 * @param attr Attribute名稱
 * @param value 元素值
 *
 * @throws IOException
 */
public void setProperty(String name, String attr, String value)
{
String nameAttr = name + ":" + attr;
propertyCache.put(nameAttr, value);

//查找指定的元素元素
Element element = this.findCreate(name);
element.setAttribute(attr, value);
saveProperties();
}
/**
 * helper方法,查找一個(gè)指定的元素,如果這個(gè)元素不存在,創(chuàng)建它
 *
 * @param name 元素名稱,格式為 X.Y.Z
 * @return Element 找到就返回這個(gè)元素,否則返回創(chuàng)建的新元素
 */
private Element findCreate(String name)
{
//分解元素的名稱
String[] propName = parsePropertyName(name);
Element element = doc.getRootElement();
//遍歷搜索匹配的元素,不存在則創(chuàng)建它
for (int i = 0; i < propName.length; i++)
{
if(element.getChild(propName[i]) == null)
{
//自動(dòng)創(chuàng)建該元素
element.addContent(new Element(propName[i]));
}
element = element.getChild(propName[i]);
}
//找到啦!
return element;
}

 /**
 * 刪除指定的元素,如果該元素不存在,不做任何事情
 *
 * @param name 要?jiǎng)h除的元素的名稱,格式為 X.Y.Z
 *
 */
public void deleteProperty(String name)
{
if(propertyCache.containsKey(name))
propertyCache.remove(name);

Element element = this.findOnly(name);
if(element != null)element.detach();
saveProperties();
}

/**
 * 刪除指定的元素的Attribute,如果該元素或者Attribute不存在,不做任何事情
 *
 * @param name 元素的名稱,格式為 X.Y.Z
 * @param attr Attribute的名稱
 *
 * @throws IOException
 */
public void deleteProperty(String name, String attr) throws IOException
{
String nameAttr = name + ":" + attr;
if(propertyCache.containsKey(nameAttr))
propertyCache.remove(nameAttr);

Element element = this.findOnly(name);
if(element != null) element.removeAttribute(attr);
saveProperties();
}


主站蜘蛛池模板: 色综合一本 | 日韩亚洲国产综合久久久 | 特级淫片国产免费高清视频 | 日本xxx在线 | 日本一区二区视频免费播放 | 亚洲图片 中文字幕 | 日本mv精品中文字幕 | 色五丁香 | 图片区偷拍区小说区 | 日韩黄色小视频 | 首页 亚洲 欧美 制服 丝腿 | 小小小小视频高清日本 | 欧美一级特黄aaaaaaa在线观看 | 天天色天天看 | 日韩精品福利视频一区二区三区 | 四虎影院免费在线播放 | 日韩视频在线免费观看 | 婷婷色天使在线视频观看 | 污污视频在线 | 最近最新中文字幕在线第一页 | 亚洲黄视频在线观看 | 五月综合激情 | 天堂网在线资源www最新版 | 最近日本免费观看视频 | 日韩精品 欧美 | 日本tv欧美tv天堂 | 四虎影院黄色 | 天天躁夜夜躁 | 一级做a爰片性色毛片小说 一级做a爰片性色毛片思念网 | 日韩免费不卡 | 日本www色视频成人免费网站 | 青草久久精品亚洲综合专区 | 欧美一区二区激情三区 | 色一欲一性一乱一区二区三区 | 四虎国产欧美成人影院 | 日本激情视频一区二区三区 | 亚洲天堂免费观看 | 亚欧视频在线 | 中文字幕第九页 | 日本免费完整版观看 | 一级毛片子 |