xml2
发布时间:2020-12-16 00:26:59 所属栏目:百科 来源:网络整理
导读:public class DealConfXml {//配置文件与Document相对应private MapString,Document map=new HashMapString,Document();private ListChangeBean list=new ArrayListChangeBean();private DocumentBuilder builder;private XPathFactory pathFactory;private
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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |