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表达式"); 查询一个节点对象
  • 相关阅读:
    CodeForcesGym 100517B Bubble Sort
    CodeForcesGym 100517H Hentium Scheduling
    BZOJ 1208: [HNOI2004]宠物收养所
    BZOJ 1503: [NOI2004]郁闷的出纳员
    BZOJ 1588: [HNOI2002]营业额统计
    sublime 3 user Settings
    sublime 3 注册码
    Why does this json4s code work in the scala repl but fail to compile?
    cat 显示指定行
    Spark Kill Application
  • 原文地址:https://www.cnblogs.com/feipengting/p/8580907.html
Copyright © 2011-2022 走看看