• Rhythmk 一步一步学 JAVA (16) dom4j 操作XML


    1、项目文件结构图:

    2、文件代码:

      doc.xml

      

    <?xml version="1.0" encoding="UTF-8"?>
    <Shop>
      <Book id="1">
        <Name>语文</Name>
        <Type>文科</Type>
      </Book>
      <Book id="2">
        <Name>
      <!-- aaaaaasfsddsfa -->  
        数学</Name>
        <Type>理科</Type>
      </Book>
      <Book id="3">
        <Name>英语</Name>
        <Type>文科</Type>
      </Book>
    </Shop>

      Dom4jMain.java

    package Dom4j.Rrhythmk.com;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream.GetField;
    import java.io.Writer;
    import java.util.Iterator;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    import org.dom4j.DocumentHelper;
    
    public class Dom4jMain {
    
        public static void main(String[] args) {
    
            String path = System.getProperty("user.dir") + "\" + "\xml\Doc.xml";
            System.out.println(path);
            ReadXML(path);
            StringToXML();
        }
    
        /*
         * 读取XML
         */
        public static void ReadXML(String path) {
    
            try {
    
                InputStream inputStream = new FileInputStream(new File(path));
    
                SAXReader saxRD = new SAXReader();
                Document doc = saxRD.read(inputStream);
                Element element = doc.getRootElement();
    
                System.out.println("getNamespace:" + element.getNamespace());
                System.out.println("节点名称-getName:" + element.getName());
                System.out.println("getText:" + element.getText());
    
                Iterator<Element> bookList = element.elements("Book").iterator();
    
                while (bookList.hasNext()) {
    
                    Element nextElement = bookList.next();
    
                    System.out.println(nextElement.attribute("id").getText());
    
                    System.out
                            .println(nextElement.element("Name").getStringValue());
    
                    System.out
                            .println(nextElement.element("Type").getStringValue());
    
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        /*
         *  字符串转换XML
         *  修改XML节点
         *  保存XML
         * */
        
        public static void StringToXML() {
            try {
                // 1、字符串转换XML
                System.out.println("StringToXML-字符串转换成XML对象");
                String xmlStr = "<Shop><Book id="1"><Name>语文</Name><Type>文科</Type></Book><Book id="2"><Name>数学</Name><Type>理科</Type></Book></Shop>";
                Document doc = DocumentHelper.parseText(xmlStr);
                Element rootElement = doc.getRootElement();
                List<Element> list = rootElement.elements("Book");
            
                for (Element e : list) {
                    System.out.println("id-" + e.attributeValue("id").toString());
    
                    System.out.println("Name-"
                            + e.element("Name").getTextTrim());
                    System.out.println("Type-"
                            + e.element("Type").getTextTrim());
                    
                    System.out.println("path-"
                            + e.element("Type").getPath());
                    
                }
                 // 2、修改XML节点
                System.out.println("修改XML节点");
                for (Element e : list) {
                    
                    e.element("Type").setText(e.element("Type").getTextTrim()+"1");
                }
                
                System.out.println("输出XML字符串");
                
                System.out.println( doc.asXML());
                 // 3、保存XML
                String path = System.getProperty("user.dir") + "\" + "\xml\Doc"+java.util.UUID.randomUUID().toString() +".xml";
                Writer  write=new FileWriter(path);
                write.write(doc.asXML());
                write.flush();
                write.close();
                System.out.println("生成XML文件路径:"+path);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        
    
    }

    输出:

    F:Java2014CodeXMLDocument\xmlDoc.xml
    getNamespace:org.dom4j.Namespace@babe [Namespace: prefix mapped to URI ""]
    节点名称-getName:Shop
    getText:


    1
    语文
    文科
    2


    数学
    理科
    3
    英语
    文科
    StringToXML-字符串转换成XML对象
    id-1
    Name-语文
    Type-文科
    path-/Shop/Book/Type
    id-2
    Name-数学
    Type-理科
    path-/Shop/Book/Type
    修改XML节点
    输出XML字符串
    <?xml version="1.0" encoding="UTF-8"?>
    <Shop><Book id="1"><Name>语文</Name><Type>文科1</Type></Book><Book id="2"><Name>数学</Name><Type>理科1</Type></Book></Shop>

  • 相关阅读:
    Dynamics CRM 请求服务时报access is denied错误
    (转载)表服务器无法打开与报表服务器数据库的连接。所有请求和处理都要求与数据库建立连接。
    如何将sqlserver的windows验证模式改为SQL Server 和 Windows 混合身份验证模式
    (转载)SQL Server2008附加数据库之后显示为只读时解决方法
    CRM导入组织报实例名称必须与计算机名称相同的问题
    The ENU localization is not supported by this SQL Server media
    js验证input输入框(字母,数字,符号,中文)
    Microsoft Dynamics CRM 2011 JS操作集锦
    一入python深似海--class
    TLB的作用及工作过程
  • 原文地址:https://www.cnblogs.com/rhythmK/p/3576832.html
Copyright © 2020-2023  润新知