首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

批改xml及重新读取

2012-12-23 
修改xml及重新读取package com.sinoufc.nms.alarm.utilimport java.io.Fileimport java.io.FileWriteri

修改xml及重新读取

package com.sinoufc.nms.alarm.util;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.net.URISyntaxException;import java.util.Iterator;import java.util.List;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;public class AnalysisAlarmPlot {private final String path = this.getClass().getResource("/").getPath()+ "alarm_plot.xml";//private static String strFileName="alarm_plot.xml";// protected static HttpServletRequest httpServletRequest; public  String analysisXML(){String refreshRate = null;  try  { // String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()+"com/sinoufc/nms/alarm/util/";  File xmlFile=new File(path);      // Assgin XML File   SAXReader reader=new SAXReader();    //Connstructor SAXReader Object    Document xmlDoc=reader.read(xmlFile);    // Read xml stream   Element root=xmlDoc.getRootElement();    //Get the root node  of XML File   List    listRowSet=xmlDoc.selectNodes("//refreshrate");   //Get the rowset   for(Iterator i=listRowSet.iterator();i.hasNext();){    Element ele=(Element)i.next();    refreshRate = ele.attribute("rate").getValue();    System.out.println(ele.attribute("rate").getValue());   }      }catch(Exception e){   System.out.print(e.getMessage());  }return refreshRate;}public  void updateXML(String refRate){  try  {//  String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()+"com/sinoufc/nms/alarm/util/";     File xmlFile=new File(path);      // Assgin XML File   SAXReader reader=new SAXReader();    //Connstructor SAXReader Object    Document xmlDoc=reader.read(xmlFile);    // Read xml stream   Element root=xmlDoc.getRootElement();    //Get the root node  of XML File   List    listRowSet=xmlDoc.selectNodes("//refreshrate");   //Get the rowset   for(Iterator i=listRowSet.iterator();i.hasNext();){    Element ele=(Element)i.next();    Attribute a=ele.attribute("rate");    ele.remove(a);                ele.addAttribute("rate",refRate);   }      XMLWriter output;           OutputFormat format = OutputFormat.createPrettyPrint();           try {               output = new XMLWriter(new FileWriter(path), format);               output.write(xmlDoc);               output.close();                         } catch (IOException e) {               e.printStackTrace();           }  }catch(Exception e){   System.out.print(e.getMessage());  }}public  void changeAlarmLisenerState(String state){try{// String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()+"com/sinoufc/nms/alarm/util/";     File xmlFile=new File(path);      // Assgin XML File   SAXReader reader=new SAXReader();    //Connstructor SAXReader Object    Document xmlDoc=reader.read(xmlFile);    // Read xml stream   Element root=xmlDoc.getRootElement();    //Get the root node  of XML File   List    listRowSet=xmlDoc.selectNodes("//alarmlisener");   //Get the rowset   for(Iterator i=listRowSet.iterator();i.hasNext();){    Element ele=(Element)i.next();    Attribute a=ele.attribute("state");    ele.remove(a);                ele.addAttribute("state",state);   }      XMLWriter output;           OutputFormat format = OutputFormat.createPrettyPrint();           try {               output = new XMLWriter(new FileWriter(path), format);               output.write(xmlDoc);                            output.close();                         } catch (IOException e) {               e.printStackTrace();           }}catch(Exception e){e.printStackTrace();}}public  String getAlarmLisenerState(){String state = null;  try  { // String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()+"com/sinoufc/nms/alarm/util/";   System.out.println(path);     File xmlFile=new File(path);      // Assgin XML File   SAXReader reader=new SAXReader();    //Connstructor SAXReader Object    Document xmlDoc=null;   xmlDoc = reader.read(xmlFile);    // Read xml stream   Element root=xmlDoc.getRootElement();    //Get the root node  of XML File   List    listRowSet=xmlDoc.selectNodes("//alarmlisener");   //Get the rowset   for(Iterator i=listRowSet.iterator();i.hasNext();){    Element ele=(Element)i.next();    state = ele.attribute("state").getValue();    System.out.println(ele.attribute("state").getValue());   }      }catch(Exception e){   System.out.print(e.getMessage());  }return state;}/* public static void main(String [] args) throws URISyntaxException{ String filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()+""; System.out.println("path:"+filePath+"com/sinoufc/nms/alarm/util/");updateXML();analysisXML(); }*/}
?

热点排行