DOM4J解析XML增加节点及内容

public static void main(String[] args) {
  SAXReader saxReader = new SAXReader();
       Document document;
  try {
     document = saxReader.read(new File("d:/text.xml"));
     Element root = document.getRootElement();
        List list = root.content();
        Element e = DocumentHelper.createElement("test");
        //增加属性
        e.addAttribute("value", "dao");
        e.addEntity("test", "测试值");
        list.add((list.size())/2, e);
        FileWriter fw = new FileWriter(new File("d:/text.xml"));
        XMLWriter output = new XMLWriter(fw);
        output.write(document);
        output.close();
  } catch (DocumentException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
   catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
 }

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭