您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Java >> 修改xml节点问题

修改xml节点问题

来源:网络整理     时间:2016/7/4 5:08:16     关键词:

关于网友提出的“ 修改xml节点问题”问题疑问,本网通过在网上对“ 修改xml节点问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 修改xml节点问题
描述:


public void update(Document doc, String title, String author) {
   NodeList nl = doc.getElementsByTagName("MenuItem");
   String strNode;
   Element e;
   try {
    for(int i = 0; i < nl.getLength(); i++) {
     e = (Element) nl.item(i);
     if(e.getAttribute("menuID").equals("recordManage")){
      e.setAttribute("menuGwdm", "getUserRoles|in|(CC)");
     } 
    }
   } catch (Exception ee) {
    ee.printStackTrace();
   }
}
public static void main(String[] args) throws ParserConfigurationException {
   magicalXML u = new magicalXML();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
   try {
    //读取源文件,如果不存在就创建一个新的给它
    File f = new File("D:/workspace/dingtingProject/src/magical123l.xml");
    Document d = db.parse(f);
    TransformerFactory tfFac = TransformerFactory.newInstance();
    Transformer tf = tfFac.newTransformer();
    tf.setOutputProperty("encoding","GB2312");
    StreamResult result = new StreamResult(System.out);
    DOMSource source = new DOMSource(d);
    tf.transform(source, result);
     u.update(d, "java basic", "dingting");
     tf.setOutputProperty("encoding","GB2312");
    tf.transform(source, result);
    tf.transform(source, new StreamResult(new File(
    "D:/workspace/dingtingProject/src/magical123l.xml")));
   } catch (SAXException e) {
    e.printStackTrace();
   } catch (FileNotFoundException e) {
    Document dl = db.newDocument();
    u.create(dl);
   } catch (TransformerConfigurationException e) {
    e.printStackTrace();
   } catch (TransformerException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
}


  
  
  
  
   
   

 
    
    
    
    
    
 



问题:实现功能就是查找节点属性是menuID="recordManage"的,更新其menuGwdm="getUserRoles|in|(C41)"该节点值,把补录业务放在工作台前面,也就是放在最前面,运行程序没问题,放到后面就更新不了,想不通是啥问题,各位帮忙看下,谢谢
解决方案1:

被注释掉的xml节点引起的


以上介绍了“ 修改xml节点问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2265056.html

相关图片

相关文章