本帖最后由 u013156418 于 2014-02-20 12:42:51 编辑

解决方案 »

  1.   


    Node common=doc.createComment("注释内容");
    doc.appendChild(common);
      

  2.   

    那如果我需要提取这两个注释内容,用正则表达式该怎么做呢?即要得到字符串:<!--
    ########################################
    @ FileName:test.xml
    @ Date:2014-02-20  10:24:24
    @ Editor:Kobe
    @ Version:1.0.0
    @ Log:这是一个测试文件
    ########################################
    -->
    <!--
    ########################################
    @ FileName:test.xml
    @ Date:2014-02-20  10:34:24
    @ Editor:Garnett
    @ Version:1.0.1
    @ Log:这是第二个注释
    ########################################
    -->
      

  3.   

    那如果我需要提取这两个注释内容,用正则表达式该怎么做呢?即要得到字符串:<!--
    ########################################
    @ FileName:test.xml
    @ Date:2014-02-20  10:24:24
    @ Editor:Kobe
    @ Version:1.0.0
    @ Log:这是一个测试文件
    ########################################
    -->
    <!--
    ########################################
    @ FileName:test.xml
    @ Date:2014-02-20  10:34:24
    @ Editor:Garnett
    @ Version:1.0.1
    @ Log:这是第二个注释
    ########################################
    -->
    Pattern pattern= Pattern.compile("<!--([\\s\\S]*)-->");
         Matcher m=pattern.matcher(xmlString);
         if(m.find()){
         log=xmlString.substring(m.start(), m.end());
         }else{
         log=null;
         }
      

  4.   

    那如果我需要提取这两个注释内容,用正则表达式该怎么做呢?即要得到字符串:<!--
    ########################################
    @ FileName:test.xml
    @ Date:2014-02-20  10:24:24
    @ Editor:Kobe
    @ Version:1.0.0
    @ Log:这是一个测试文件
    ########################################
    -->
    <!--
    ########################################
    @ FileName:test.xml
    @ Date:2014-02-20  10:34:24
    @ Editor:Garnett
    @ Version:1.0.1
    @ Log:这是第二个注释
    ########################################
    -->
    Pattern pattern= Pattern.compile("<!--([\\s\\S]*)-->");
         Matcher m=pattern.matcher(xmlString);
         if(m.find()){
         log=xmlString.substring(m.start(), m.end());
         }else{
         log=null;
         }
    忘了回帖,确实可以,感谢4楼!