zoukankan      html  css  js  c++  java
  • Java 中xml解析

    1.String 字符串保持到txt文件

    String xml ="abcdefghijk";
    		  FileWriter fw = null;
    		  File f = new File("d:\a.txt");
    		   try {
    		    if(!f.exists()){
    		     f.createNewFile();
    		    }
    		    fw = new FileWriter(f);
    		    BufferedWriter out = new BufferedWriter(fw);
    		    out.write(xml, 0, xml.length()-1);
    		    out.close();
    		   } catch (IOException e) {
    		    e.printStackTrace();
    		   }
    

    2.读取xml节点,获取节点值,节点属性等

    导入的包

    import org.dom4j.Document;

    import org.dom4j.io.SAXReader;
    SAXReader reader = new SAXReader();
    Document doc = reader.read(new File("d:/dahua.xml"));
    doc = DocumentHelper.parseText("字符串");//String 直接转为 Document 方法
    doc.read();//可以读取file,inputstream等
    //节点: Iterator Element.nodeIterator(); //获取当前标签节点下的所有子节点 //标签: Element Document.getRootElement(); //获取xml文档的根标签 Element ELement.element("标签名") //指定名称的第一个子标签 Iterator<Element> Element.elementIterator("标签名");// 指定名称的所有子标签 List<Element> Element.elements(); //获取所有子标签 //属性: String Element.attributeValue("属性名") //获取指定名称的属性值 Attribute Element.attribute("属性名");//获取指定名称的属性对象 Attribute.getName() //获取属性名称 Attibute.getValue() //获取属性值 List<Attribute> Element.attributes(); //获取所有属性对象 Iterator<Attribute> Element.attibuteIterator(); //获取所有属性对象 //文本: Element.getText(); //获取当前标签的文本 Element.elementText("标签名") //获取当前标签的指定名称的子标签的文本内容 // 使用xpath方法 List<Node> selectNodes("xpath表达式"); 查询多个节点对象 Node selectSingleNode("xpath表达式"); 查询一个节点对象
  • 相关阅读:
    http协议相关知识
    linux 常用命令总结
    PHP traits
    php 正则案例
    php 中关于正则 元字符
    【U3D】 第三人称控制器ThirdPersonCharacter添加之后角色原地打转不移动的问题(unity5.3.5f)
    .Net Core异步async/await探索
    IdentityServer4实现单点登录统一认证
    CSAPP-Tiny Web服务器【2】源码解析
    CSAPP-Tiny Web服务器【1】编译搭建
  • 原文地址:https://www.cnblogs.com/feipengting/p/8580907.html
Copyright © 2011-2022 走看看