java对xml文件的操作
xml文件格式(示例):
<?xml version="1.0" encoding="UTF-8"?> <root>
<FunctionKeySettings enabled="true">
<FuncAssign id="0" selected="true">
<Display>System Home</Display>
<FuncKeys>
<Func selected="true">System Home</Func>
<Func>EZ Charger Suite</Func>
<Func>RSI Home</Func>
<Func>Web Browser NX</Func>
<Func>ESA Transformer</Func>
<Func>Print/Scan (Memory Storage Device)</Func>
<Func>Printer (Smart)</Func>
<Func>Printer (Classic)</Func>
<Func>Copy (Smart)</Func>
<Func>Copier (Classic)</Func>
<Func>Scanner (Smart)</Func>
<Func>Scanner (Classic)</Func>
</FuncKeys>
</FuncAssign>
<FuncAssign id="1" selected="true">
<Display>RSI Homerrrrr</Display>
<FuncKeys>
<Func>System Home</Func>
<Func>EZ Charger Suite</Func>
<Func selected="true">RSI Home</Func>
<Func>Web Browser NX</Func>
<Func>ESA Transformer</Func>
<Func>Print/Scan (Memory Storage Device)</Func>
<Func>Printer (Smart)</Func>
<Func>Printer (Classic)</Func>
<Func>Copy (Smart)</Func>
<Func>Copier (Classic)</Func>
<Func>Scanner (Smart)</Func>
<Func>Scanner (Classic)</Func>
</FuncKeys>
</FuncAssign>
<FuncAssign id="2" selected="true">
<Display>EZ Charger Suite</Display>
<FuncKeys>
<Func>System Home</Func>
<Func selected="true">EZ Charger Suite</Func>
<Func>RSI Home</Func>
<Func>Web Browser NX</Func>
<Func>ESA Transformer</Func>
<Func>Print/Scan (Memory Storage Device)</Func>
<Func>Printer (Smart)</Func>
<Func>Printer (Classic)</Func>
<Func>Copy (Smart)</Func>
<Func>Copier (Classic)</Func>
<Func>Scanner (Smart)</Func>
<Func>Scanner (Classic)</Func>
</FuncKeys>
</FuncAssign>
</FunctionKeySettings>
<WebBrowserNXSettings enabled="true">
<HomePage>https://www.na.smart-integration.ricoh.com/si-apps/pub/index.html</HomePage>
</WebBrowserNXSettings>
</root>
对文件的操作:更新、新建、实例化
package com.ricoh.rapp.deploymenttool.device; import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element; import com.ricoh.rapp.deploymenttool.ui.RSICloudModel;
import com.ricoh.rapp.deploymenttool.ui.RSICloudModel.FunctionKey;
import com.ricoh.rapp.deploymenttool.ui.RSICloudModel.FunctionKeyOptions;
import com.ricoh.rapp.deploymenttool.util.Consts;
import com.ricoh.rapp.deploymenttool.util.XmlParser; public class RSICloudXmlParser extends XmlParser{ private static final Log logger = LogFactory.getLog(RSICloudXmlParser.class); private static final File file = new File(Consts.RSICLOUD_FILE_PATH);
private static final File initialFile = new File(Consts.RSICLOUD_INITIAL_FILE_PATH); public static final String initialUrl = "https://www.na.smart-integration.ricoh.com/si-apps/pub/index.html"; private static final String ELEMENT_ROOT = "root";
private static final String ELEMENT_FUNCTIONKEY_SETTINGS = "FunctionKeySettings";
private static final String ELEMENT_FUNCASSIGN = "FuncAssign";
private static final String ELEMENT_DISPLAY = "Display";
private static final String ELEMENT_FUNCKEYS = "FuncKeys";
private static final String ELEMENT_FUNC = "Func";
private static final String ELEMENT_WEBBROWSER_NXSETTINGS = "WebBrowserNXSettings";
private static final String ELEMENT_HOMEPAGE = "HomePage";
private static final String ATTR_ENABLED = "enabled";
private static final String ATTR_ID = "id";
private static final String ATTR_SELECTED = "selected";
private static final String ATTR_VALUE_TRUE = "true";
private static final String ATTR_VALUE_FALSE = "false"; public static RSICloudModel parseRSICloudXML(){
logger.info("parseRSICloudXML file: " + initialFile.getName());
logger.info("parseRSICloudXML file: " + file.getName());
Document document = null;
if(file.exists()) {
document = readXML(file);
}else {
document = readXML(initialFile);
} return xmlFileToBean(document);
} public static RSICloudModel resetParseRSICloudXML(){
logger.info("parseRSICloudXML file: " + initialFile.getName());
Document document = readXML(initialFile);
return xmlFileToBean(document);
} private static RSICloudModel xmlFileToBean(Document document) {
RSICloudModel rsiCloud = new RSICloudModel();
if(document == null) {
return rsiCloud;
}
Element root = document.getRootElement();
if(root == null) {
return rsiCloud;
}
Element fkSettingsEle = root.element(ELEMENT_FUNCTIONKEY_SETTINGS);
Element wbSettingsEle = root.element(ELEMENT_WEBBROWSER_NXSETTINGS);
if (fkSettingsEle == null && wbSettingsEle == null) {
return rsiCloud;
} String fkEnabled = fkSettingsEle.attributeValue(ATTR_ENABLED);
rsiCloud.setFunctionKeySettings(stringToBln(fkEnabled));
List<FunctionKey> functionKeys = new ArrayList<>();
for(Iterator rsiCloudsIterator = fkSettingsEle.elementIterator(ELEMENT_FUNCASSIGN); rsiCloudsIterator.hasNext();) {
Element funcAssignEle = (Element) rsiCloudsIterator.next();
FunctionKey FunctionKey = rsiCloud.new FunctionKey();
String id = funcAssignEle.attributeValue(ATTR_ID);
String isSelected = funcAssignEle.attributeValue(ATTR_SELECTED);
String dispalyName = funcAssignEle.element(ELEMENT_DISPLAY).getText(); FunctionKey.setId(Integer.parseInt(id));
FunctionKey.setFuncAssign(stringToBln(isSelected));
FunctionKey.setDispalyName(dispalyName);
List<FunctionKeyOptions> functionKeyOptions = new LinkedList<>();
for(Iterator funcIterator = funcAssignEle.element(ELEMENT_FUNCKEYS).elementIterator(ELEMENT_FUNC); funcIterator.hasNext();) {
Element funcEle = (Element) funcIterator.next();
FunctionKeyOptions functionKeyOption = rsiCloud.new FunctionKeyOptions();
String selectedOpt = funcEle.attributeValue(ATTR_SELECTED);
String optValue = funcEle.getText();
functionKeyOption.setSelected(stringToBln(selectedOpt));
functionKeyOption.setChoiceValues(optValue);
functionKeyOptions.add(functionKeyOption);
}
FunctionKey.setFunctionKeyOptions(functionKeyOptions); functionKeys.add(FunctionKey);
} rsiCloud.setFunctionKeys(functionKeys);
String wbEnabled = wbSettingsEle.attributeValue(ATTR_ENABLED);
rsiCloud.setWebBrowserNXSettings(stringToBln(wbEnabled));
String homePageUrl = wbSettingsEle.element(ELEMENT_HOMEPAGE).getText();
rsiCloud.setHomePageUrl(homePageUrl);
return rsiCloud;
} public static boolean saveCustomSettings(RSICloudModel rsiCloud) {
if(file.exists()) {
return updateRSICloudXML(rsiCloud);
}else {
return createRSICloudXml(rsiCloud);
}
} public static boolean updateRSICloudXML(RSICloudModel rsiCloud) {
Document document = readXML(file);
if(document == null || rsiCloud == null) {
return false;
}
Element root = document.getRootElement(); Element fkSettingsEle = root.element(ELEMENT_FUNCTIONKEY_SETTINGS); boolean functionKeySettings = rsiCloud.isFunctionKeySettings();
fkSettingsEle.addAttribute(ATTR_ENABLED, booleanToStr(functionKeySettings)); List<FunctionKey> functionKeys = rsiCloud.getFunctionKeys(); for(Iterator rsiCloudsIterator = fkSettingsEle.elementIterator(ELEMENT_FUNCASSIGN); rsiCloudsIterator.hasNext();) {
Element funcAssignEle = (Element) rsiCloudsIterator.next(); int id = Integer.parseInt(funcAssignEle.attributeValue(ATTR_ID)); for(FunctionKey functionKey : functionKeys) {
if(functionKey.getId() == id) {
boolean isSelected = functionKey.isFuncAssign();
funcAssignEle.addAttribute(ATTR_SELECTED, booleanToStr(isSelected));
String displayName = functionKey.getDispalyName().trim();
funcAssignEle.element(ELEMENT_DISPLAY).setText(displayName); for(Iterator funcIterator = funcAssignEle.element(ELEMENT_FUNCKEYS).elementIterator(ELEMENT_FUNC); funcIterator.hasNext();) {
Element funcEle = (Element) funcIterator.next();
String funcValue = funcEle.getText();
List<FunctionKeyOptions> functionKeyOptions = functionKey.getFunctionKeyOptions();
for(FunctionKeyOptions functionKeyOption : functionKeyOptions) {
if(functionKeyOption.isSelected() && funcValue.trim().equals(functionKeyOption.getChoiceValues().trim())) {
funcEle.addAttribute(ATTR_SELECTED, booleanToStr(true));
} if(!functionKeyOption.isSelected() && funcValue.trim().equals(functionKeyOption.getChoiceValues().trim())) {
funcEle.remove(funcEle.attribute(ATTR_SELECTED));
}
}
}
}
}
} Element wbSettingsEle = root.element(ELEMENT_WEBBROWSER_NXSETTINGS);
boolean webBrowserSetting = rsiCloud.isWebBrowserNXSettings();
wbSettingsEle.addAttribute(ATTR_ENABLED, booleanToStr(webBrowserSetting));
String homePageUrl = rsiCloud.getHomePageUrl();
wbSettingsEle.element(ELEMENT_HOMEPAGE).setText(homePageUrl); writeXML(document, file); return true;
} private static boolean createRSICloudXml(RSICloudModel rsiCloud) {
if(file.isFile() && file.exists()) {
return true;
}else {
try {
file.createNewFile();
} catch (IOException e) {
logger.error("createRSICloudXml failed:File name: " + file.getName() + ", File path:" + file.getAbsolutePath()
+ ", IOException:" + e.getMessage());
}
}
if(rsiCloud == null) return false; Document document = DocumentHelper.createDocument(); Element root = document.addElement(ELEMENT_ROOT);
Element fkSettingsEle = root.addElement(ELEMENT_FUNCTIONKEY_SETTINGS);
if(rsiCloud.isFunctionKeySettings()) {
fkSettingsEle.addAttribute(ATTR_ENABLED, ATTR_VALUE_TRUE);
}else {
fkSettingsEle.addAttribute(ATTR_ENABLED, ATTR_VALUE_FALSE);
} for(FunctionKey functionKey : rsiCloud.getFunctionKeys()) {
Element funcAssignEle = fkSettingsEle.addElement(ELEMENT_FUNCASSIGN);
funcAssignEle.addAttribute(ATTR_ID, String.valueOf(functionKey.getId()));
funcAssignEle.addAttribute(ATTR_SELECTED, ATTR_VALUE_TRUE);
if(functionKey.isFuncAssign()) {
funcAssignEle.addAttribute(ATTR_SELECTED, ATTR_VALUE_TRUE);
}else {
funcAssignEle.addAttribute(ATTR_SELECTED, ATTR_VALUE_FALSE);
} Element displayEle = funcAssignEle.addElement(ELEMENT_DISPLAY);
displayEle.setText(functionKey.getDispalyName()); Element funcKeysEle = funcAssignEle.addElement(ELEMENT_FUNCKEYS);
for(FunctionKeyOptions functionKeyOption :functionKey.getFunctionKeyOptions()) {
Element funcEle = funcKeysEle.addElement(ELEMENT_FUNC);
funcEle.setText(functionKeyOption.getChoiceValues());
if(functionKeyOption.isSelected()) funcEle.addAttribute(ATTR_SELECTED, ATTR_VALUE_TRUE);
} } Element webBrowSettingsEle = root.addElement(ELEMENT_WEBBROWSER_NXSETTINGS);
if(rsiCloud.isWebBrowserNXSettings()) {
webBrowSettingsEle.addAttribute(ATTR_ENABLED, ATTR_VALUE_TRUE);
}else {
webBrowSettingsEle.addAttribute(ATTR_ENABLED, ATTR_VALUE_FALSE);
}
Element homePageEle = webBrowSettingsEle.addElement(ELEMENT_HOMEPAGE);
homePageEle.setText(rsiCloud.getHomePageUrl()); return writeXML(document, file);
} public static boolean stringToBln(String str) {
if(str != null && ATTR_VALUE_TRUE.equals(str.trim())) {
return true;
}
return false;
} public static String booleanToStr(boolean bln) {
if(bln) {
return ATTR_VALUE_TRUE;
}else {
return ATTR_VALUE_FALSE;
}
} public static void main(String[] args) throws IOException {
/*RSICloudModel rsiCloud = RSICloudXmlParser.parseRSICloudXML();
for(FunctionKey functionKey: rsiCloud.getFunctionKeys()) {
if(functionKey.getId() == 0) {
functionKey.getFunctionKeyOptions().toArray();
}
}*/ // createRSICloudXml(null);
System.out.println(initialUrl);
} }
java对xml文件的操作的更多相关文章
- 更新java对xml文件的操作
//更新java在xml文件中操作的内容 public static void upda(Document doc) throws Exception{ //创建一个TransformerFactor ...
- Java 创建xml文件和操作xml数据
java中的代码 import java.io.File; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; ...
- # java对xml文件的基本操作
下面是简单的总结三种常用的java对xml文件的操作 1. dom方式对xml进行操作,这种操作原理是将整个xml文档读入内存总,在内存中进行操作,当xml文档非常庞大的时候就会出现内存溢出的异常,这 ...
- JAVA读取XML文件并解析获取元素、属性值、子元素信息
JAVA读取XML文件并解析获取元素.属性值.子元素信息 关键字 XML读取 InputStream DocumentBuilderFactory Element Node 前言 最 ...
- java对xml文件做增删改查------摘录
java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...
- java 读取XML文件作为配置文件
首先,贴上自己的实例: XML文件:NewFile.xml(该文件与src目录同级) <?xml version="1.0" encoding="UTF-8&quo ...
- Java 字节流实现文件读写操作(InputStream-OutputStream)
Java 字节流实现文件读写操作(InputStream-OutputStream) 备注:字节流比字符流底层,但是效率底下. 字符流地址:http://pengyan5945.iteye.com/b ...
- java读XML文件
XML文件设计为传输和存储数据,其焦点为数据内容. HTML设计为用来显示数据, 其焦点为数据外观. XML仅仅是文本文件,任何文本编辑器一般情况下都能对其进行编辑. XML没有预定义的标签,并且设定 ...
- java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .
背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...
随机推荐
- Blob检测
一 Laplace 算子 使用一阶微分算子可以检测图像边缘.对于剧烈变化的图像边缘,一阶微分效果比较理想.但对于缓慢变化的图像边缘,通过对二阶微分并寻找过零点可以很精确的定位边缘中心.二阶微分即为 L ...
- 五、模板方法设计模式及在Spring中的应用
模板方法模式是一种行为型设计模式,具体定义网络上很多资源搜到本文不赘述. 如果字面理解比较抽象的话,那以生活中简单的行为为例:天热了,到了晚上妈妈都要将今天没有吃完的饭菜放入冰箱.将饭菜放入冰箱就是一 ...
- Python面向对象之 - 继承
情况一: 子类完全继承父类所有的属性和方法, 自己没有一点更改. class Person(): def __init__(self, name, age): self.name = name se ...
- 攻防世界Web_shrine
题目: 给的是源代码,整理一下如下: 看到jinjia flask,render_template_string猜测到这题应该是考查python模板注入. 代码分析可以得到以下信息: 1.路径在 /s ...
- [故障]ceph存储池权限修改错误,导致存储池的业务hang住
描述: 记录一次重大事故:根据IaaS资源业务要求,需要增加某些功能,所以要修改部署代码.修改后重推部署代码,检查发现没有什么异常. 但是一段时间后就收到用户的报障反馈,接连一个电话.2个电话.3个电 ...
- 数据透视:Excel数据透视和Python数据透视
作者 | leo 早于90年代初,数据透视的概念就被提出,主要的应用场景是处理大量数据的交互式汇总查询,它实现了行或列的移动,使得行可以移到列上,列移到行上,从而根据使用者的诉求取对关注的数据子集进行 ...
- 【C# .Net GC】垃圾回收算法 应用程序线程运行时,
触发垃圾回收算法的条件 触发垃圾回收的条件 当满足以下条件之一时将发生垃圾回收: 操作系统报告低内存请看(将触发第2代垃圾回收). 这是通过 OS 的内存不足通知或主机指示的内存不足检测出来. 由托管 ...
- 【windows 操作系统】进程
前言 Windows的内部实现也近似于"一切皆文件"的思想,当然,这一切都只在内核里才有,下载一个WinObj这软件就可以看到,Windows上各种设备.分区.虚拟对象都是挂载到根 ...
- 每日一算之变位词(C#)
今天看编程珠玑里面,看到一个关于查找变位词的题目,大概意思如下:post,stop,tops这几个是变位词,找出类似的这些词语来. 解题思路一:既然是变位词,1.他们的长度一定是一致的:2.还有就是他 ...
- 踢出某正在访问的用户||永久禁止某IP访问
转至:https://blog.csdn.net/weixin_34408717/article/details/85527305?utm_medium=distribute.pc_aggpage_s ...