📄 xmloperator.java
字号:
package com.zte.cdma.autotest.mulautotest.buildxml;
//package buildxml;
import com.zte.org.jdom.*;
import java.util.*;
import java.io.*;
import com.zte.org.jdom.adapters.XercesDOMAdapter;
import com.zte.org.jdom.input.DOMBuilder;
import com.zte.org.jdom.output.XMLOutputter;
import com.zte.org.jdom.input.SAXBuilder;
//import com.zte.test.util.string.StrUtil;
/**
*<p>版权所有(C)2002,深圳市中兴通讯股份有限公司</p>
*
*<p>文件名称:XMLOperator.java</p>
*<p>文件标识:</p>
*<p>内容摘要:实现xml文件的操作类,完成对xml节点属性值或text的修改、删除以及节点的屏蔽等操作</p>
*<p>当前版本://输入当前版本</p>
*<p>作 者:鄢彪</p>
*<p>完成日期:输入完成日期,例如:2002-10-11</p>
*
*<p>修改记录1:修改历史记录,包括修改日期、修改者及修改内容</p>
*<pre>
* 修改日期:
* 版 本 号:
* 修 改 人:
* 修改内容:
*</pre>
*/
public class XMLOperator {
//fileName 记录输入文件的名字
private String xmlFileName = null;
//记录xml文件的全路径
private String xmlAbsoluteFile = null;
//记录某一个xml的document
private Document doc = null;
private boolean isNewFile = false;
/**
* 构造函数,当建立此函数时,同时打开该文件.
* @param inFileName String 输入的文件全路径名
*/
public XMLOperator(String inFileName) throws IOException, JDOMException {
init(inFileName);
}
/**
* 初始化函数,完成对目标文件的JDOM解析,如果发现不存在,则创建一个新的Document,并设置
* isNewFile为true.
* @param inFileName String
* @throws IOException
* @throws JDOMException
*/
private void init(String inFileName) throws IOException, JDOMException {
if (inFileName == null || inFileName.trim().equals("")) {
return;
}
File xmlFile = new File(inFileName);
xmlAbsoluteFile = xmlFile.getAbsolutePath();
xmlFileName = xmlFile.getName();
java.net.URL url = Thread.currentThread().getContextClassLoader().
getResource(inFileName.trim().replace('\\', '/'));
if (!xmlFile.exists() && url == null) {
System.out.println("!exists");
xmlFile = new File(xmlFile.getAbsolutePath());
File xmlPathFile = new File(xmlFile.getParent());
if (!xmlPathFile.exists() && !xmlPathFile.mkdirs()) {
throw new IOException("can't create such directory:" +
inFileName);
}
Element root = new Element("undefined");
doc = new Document(root);
isNewFile = true;
}
else {
// XercesDOMAdapter xercAdapter = new XercesDOMAdapter();
// org.w3c.dom.Document w3Dom = xercAdapter.getDocument(
// new FileInputStream(inFileName), false);
SAXBuilder builder = new SAXBuilder();
if (url != null) {
doc = builder.build(url.openStream());
}
else if (xmlFile.exists()) {
doc = builder.build(xmlFile);
}
}
}
/**
* 获取根节点
* @return Element
*/
public Element getRoot() {
if (doc == null)
return null;
return doc.getRootElement();
}
/**
* 设置根节点
* @param root Element
*/
public void setRoot(Element root) {
doc.setRootElement(root);
}
/**
* 将一个document写入一个xml文件中
* @param filename
* @throws IOException
*/
public void saveAs(String filename) throws IOException {
if (filename == null || filename.trim().equals(""))
return;
File tempFile = new File(filename);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
FileWriter xmlWrite = new FileWriter(filename);
// com.zte.org.jdom.output.Format format = com.zte.org.jdom.output.Format.getPrettyFormat();
// format.setEncoding("GB2312");
// format.set
XMLOutputter domstream = new XMLOutputter();
domstream.setEncoding("GB2312");
domstream.setIndent(" ");
domstream.setNewlines(true);
// domstream.setTrimAllWhite(true);
domstream.output(doc, xmlWrite);
xmlWrite.close();
}
/**
* 将一个document写入一个xml文件中
* @param filename
* @throws IOException
*/
public void save() throws IOException {
FileWriter xmlWrite = new FileWriter(xmlAbsoluteFile);
// com.zte.org.jdom.output.Format format = com.zte.org.jdom.output.Format.getPrettyFormat();
// format.setEncoding("GB2312");
XMLOutputter domstream = new XMLOutputter();
domstream.setEncoding("GB2312");
domstream.setIndent(" ");
domstream.setNewlines(true);
domstream.output(doc, xmlWrite);
xmlWrite.close();
}
/**
* 修改配置文件中节点值,此函数只处理单个匹配修改的情况,如果filterStr没有定义,则把replaceStr
* 的值覆盖xPath所定位的元素值.
* @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
* @param filterStr String 符合java的patern格式, 即()[]{}*.$@+?^!都有特殊含义,在匹配时需要考虑
* @param replaceStr String 需要替换的字符串组,对应匹配字符数组
* @return boolean
*/
public boolean modifyNode(String xPath, String filterStr, String replaceStr) {
if (filterStr == null || filterStr.trim().equals("")) {
return modifyNode(xPath, replaceStr);
}
String[] filters = new String[] {
filterStr};
String[] replaceStrs = new String[] {
replaceStr};
return modifyNode(xPath, filters, replaceStrs);
}
/**
* 修改配置文件中节点值,此函数同时处理多个匹配替换的情况.
* @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
* @param filterStrArray String[] 存放匹配字符组,其中字符是"*"为通配,"?"为任何一个字符
* @param replaceStrArray String[] 需要替换的字符串组,对应匹配字符数组
* @return boolean 如果修改完成则返回true
*/
public boolean modifyNode(String xPath, String[] filterStrArray,
String[] replaceStrArray) {
try {
com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
xPath);
List params = xpathDom.selectNodes(doc);
for (int i = 0; i < params.size(); i++) {
//如果取得的是元素节点,则说明要对text进行操作
if (params.get(i) instanceof Element) {
Element node = (Element) params.get(i);
String new_Text = StrUtil.replaceStr(node.getText(),
filterStrArray, replaceStrArray);
node.setText(new_Text);
}
//如果获取到是属性对象,则说明要对属性值进行修改
else if (params.get(i) instanceof Attribute) {
Attribute attr = (Attribute) params.get(i);
String new_Text = StrUtil.replaceStr(attr.getValue(),
filterStrArray, replaceStrArray);
attr.setValue(new_Text);
}
}
return true;
}
catch (JDOMException ex) {
ex.printStackTrace();
return false;
}
}
/**
* 修改指定节点的值,把原来的值删除在设置为value指定的值
* @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
* @param value String 需要替换的值
* @return boolean 如果成返回true,否则返回false
*/
public boolean modifyNode(String xPath, String value) {
try {
com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
xPath);
List params = xpathDom.selectNodes(doc);
for (int i = 0; i < params.size(); i++) {
//如果取得的是元素节点,则说明要对text进行操作
if (params.get(i) instanceof Element) {
Element node = (Element) params.get(i);
node.setText(value);
}
//如果获取到是属性对象,则说明要对属性值进行修改
else if (params.get(i) instanceof Attribute) {
Attribute attr = (Attribute) params.get(i);
attr.setValue(value);
}
}
return true;
}
catch (JDOMException ex) {
ex.printStackTrace();
return false;
}
}
/**
* 删除某一个节点
* @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
* @return boolean 如果删除完成则返回true
*/
public boolean deleteNode(String xPath) {
try {
com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
xPath);
List params = xpathDom.selectNodes(doc);
for (int i = 0; i < params.size(); i++) {
//如果取得的是元素节点,则说明要对text进行操作
if (params.get(i) instanceof Element) {
Element node = (Element) params.get(i);
node.detach();
}
//如果获取到是属性对象,则说明要对属性值进行修改
else if (params.get(i) instanceof Attribute) {
Attribute attr = (Attribute) params.get(i);
attr.detach();
}
}
return true;
}
catch (JDOMException ex) {
ex.printStackTrace();
return false;
}
}
/**
* 在指定的xpath下增加一个定义的孩子节点
* @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
* @param nodeDef String 节点定义字符串
* (如"game[@name='clouds',@location='http://clouds.com.cn',text()='this is one interesting game']"
* @return boolean
*/
// public boolean addChild(String xPath, String nodeDef) {
// return addChild(xPath, nodeDef, -1);
// }
/**
* 在指定的xpath下增加一个定义的孩子节点
* @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
* @param nodeDef String 节点定义字符串
* (如"game[@name='clouds',@location='http://clouds.com.cn',text()='this is one interesting game']"
* @param index int 指定孩子的位置,假如是一个非法的数字则加入到最后.
* @return boolean 如果增加完成则返回true
*/
// public boolean addChild(String xPath, String nodeDef, int index) {
// try {
// com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
// xPath);
// List params = xpathDom.selectNodes(doc);
// if (params.get(0) instanceof Element) {
// Element node = (Element) params.get(0);
// List children = node.getChildren();
// int elementPos = children.size();
// if (children.size() == 0) {
// elementPos = 0;
// }
// else if (index < 0 || index >= children.size()) {
// elementPos = node.indexOf((Element) children.get(children.
// size() - 1)) + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -