public class DealConfXml {
//配置文件与Document相对应
private Map<String,Document> map=new HashMap<String,Document>();
private List<ChangeBean> list=new ArrayList<ChangeBean>();
private DocumentBuilder builder;
private XPathFactory pathFactory;
private XPath xpath;
private Document doc;
//基路径
private String basePath="src/";
public DealConfXml(){
try {
init();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
//加载主配置文件
public void init() throws Exception{
//创建DocumentFactory 和DocumentBuilder
builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
//根据地址构建document
doc=builder.parse("src/encode2.xml");
pathFactory = XPathFactory.newInstance();
xpath = pathFactory.newXPath();
NodeList nodeList=doc.getElementsByTagName("file");
if(nodeList!=null&&nodeList.getLength()>0){
for(int i=0;i<nodeList.getLength();i++){
Node node=nodeList.item(i);
String filepath=xpath.compile("filepath/text()").evaluate(node, XPathConstants.STRING).toString();
String nodepath=xpath.compile("nodepath/text()").evaluate(node, XPathConstants.STRING).toString();
String nodevalue=xpath.compile("nodevalue/text()").evaluate(node, XPathConstants.STRING).toString();
String descript=xpath.compile("descript/text()").evaluate(node, XPathConstants.STRING).toString();
String type=xpath.compile("type/text()").evaluate(node, XPathConstants.STRING).toString();
ChangeBean changeBean=new ChangeBean(filepath,nodepath,nodevalue,descript,type);
System.out.println(changeBean);
list.add(changeBean);
}
}
}
//根据changeBean 加载相关的配置文件的document
public void initMap() throws SAXException, IOException,Exception{
for(int i=0;i<list.size();i++){
ChangeBean changeBean=list.get(i);
//避免重复加载配置文件
if(map.keySet().contains(changeBean.getFilepath())){
continue;
}
Document document=builder.parse(basePath+changeBean.getFilepath());
map.put(changeBean.getFilepath(), document);
}
}
//根据主配置文件修改其它的配置文件
public void update() throws XPathExpressionException{
for(int i=0;i<list.size();i++){
ChangeBean changeBean=list.get(i);
Object result=xpath.compile(changeBean.getNodepath()).evaluate(map.get(changeBean.getFilepath()),XPathConstants.NODE );
Element node=(Element)result;
if("0".equals(changeBean.getType())){
node.getFirstChild().setNodeValue(changeBean.getNodevalue());
}else if("1".equals(changeBean.getType())){
node.setAttribute(getAttribute(changeBean.getNodepath()), changeBean.getNodevalue());
}
System.out.println(node.getFirstChild().getNodeValue());
}
}
//将xml的修改保存到文件
public void save() throws Exception{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Set<String> key=map.keySet();
Iterator<String> iterator=key.iterator();
while(iterator.hasNext()){
String filepath=iterator.next();
StreamResult streamResult = new StreamResult(basePath+filepath);
DOMSource domSource = new DOMSource(map.get(filepath));
Properties property=transformer.getOutputProperties();
property.setProperty(OutputKeys.ENCODING, "UTF-8");
property.setProperty(OutputKeys.INDENT, "yes");//设置缩进
transformer.setOutputProperties(property);
transformer.transform(domSource, streamResult);
}
}