zoukankan      html  css  js  c++  java
  • Java Dom4j XML用法总结

    1、新建XML文档:

                 Document doc = DocumentHelper.createDocument();
                Element root = doc.addElement( "ocs");
                root.addElement( "header");
                Element body = root.addElement( "body");
                Element fontnode = body.addElement( "font");
                fontnode.addAttribute( "size", "9");
                System. out.println(root.asXML());
     
    2、打开现有XML文档(从字符串导入):
               Document doc = DocumentHelper.parseText(XMLStr);
                Node node = doc.selectSingleNode( "ocs/body/font");    
                 if (node instanceof Element)
                {
                      Element pnode = (Element) node;           
                      Attribute attr = pnode.attribute( "size");
                      System. out.println(attr.asXML());
                }
     
              Dom4j中内置了XPath,因此可以在selectSingleNode中象XPath那样访问路径字符串;
              注意:需要引用jaxen-1.1.1.jar包,否则selectSingleNode会产生异常并返回Null;
     
    3、打开现有文档(从文件导入):
              SAXReader saxReader = new SAXReader();
              Document document = saxReader.read( XMLFile);
     
    4、将Document写入到文件中:
              try
                {
                      XMLWriter output = new XMLWriter( new FileWriter( new File(XMLFile)));
                      output.write( doc);
                      output.close();
                } catch (IOException e)
                {
                      System. out.println(e.getMessage());
                }
  • 相关阅读:
    CSS学习笔记 糖不苦
    Servlet与HTTP介绍学习 糖不苦
    new 的原理和实现 糖不苦
    HTML学习笔记 糖不苦
    事务的概念,以及事务在JDBC编程中处理事务的步骤 糖不苦
    前端JS获取用户位置 糖不苦
    数据接口请求异常:parsererror 糖不苦
    所有CSS字体属性 糖不苦
    jQuery CSS样式方法
    jQuery效果隐藏/显示,淡入/淡出,滑动,动画
  • 原文地址:https://www.cnblogs.com/laoxia/p/4670080.html
Copyright © 2011-2022 走看看