• 17 | xml


    1 xml

    可扩展标记语言,首先是一种语言

    2 作用

    • 数据交互
    • 配置应用
    • AJAX的基石

    3 xml的元素

    • <元素名 属性名=“属性值”>元素内容
    • 正常情况下,有元素内容,那么元素除了开始,还要有闭合
    • 但是没有元素内容的情况下,可以写为自闭合标签形式<元素名 属性=“属性值/”>
    • 所有xml标签元素都必须有结束标签
    • xml标签对大小写敏感
    • xml必须正确的嵌套
    • 同级标签以缩进对齐
    • 元素名称可以包含字母,数字或者其他的字符
    • 元素名称不能以数字或标点符号开始
    • 元素名称中不能含空格
    • :只能用于文本
    • 在编辑xml的时候,完全可以遵循面向对象的基本思路

    4 xml的基本结构

    4.1 节点

    在xml当中,所有的内容,都是节点

    4.2 文档节点

    • xml文件本身,就是文档节点
    • 名称:Document
    • 类型:9
    • 值:无

    4.3 元素节点

    • 标签
    • 名称:element
    • 类型:1
    • 值:无

    4.4 属性节点

    • 属性定义在[标签/元素] 当中
    • 名称:attribute
    • 类型:2
    • 值:有值

    4.5 文本节点

    • 在元素当中
    • 名称
    • 类型

    4.6 注释节点

    4.6.1 实例1

    demo.xml
      
      
    <clothesList>  
      
          <clothes  id="1" type="s" height="<165">  
          clothes>  
          <clothes  id="2" type="s" maxHeight="165" minHeight="">  
          clothes>  
          <clothes  id="3" type="s">        
                <height maxHeight="165" minHeight=""/>  
          clothes>  
          <clothes  id="4" type="S">        
                <height name="max" minHeight="165"/>  
                <height name="min" minHeight=""/>  
                <explain>  
                     
                explain>  
         clothes>  
          <clothes  id="5" type="M">        
                <height name="max" minHeight="170"/>  
                <height name="min" minHeight="165"/>  
                           <explain>  
                     
                explain>  
          clothes>  
          <clothes  id="6" type="L">        
                <height name="max" minHeight="175"/>  
                <height name="min" minHeight="170"/>  
                            <explain>  
                     
                explain>  
          clothes>  
          <clothes  id="7" type="XL">        
                <height name="max" minHeight="180"/>  
                <height name="min" minHeight="175"/>  
                            <explain>  
                     
                explain>  
          clothes>  
          <clothes  id="8" type="XXL">        
                <height name="max" minHeight="185"/>  
                <height name="min" minHeight="180"/>  
                            <explain>  
                     
                explain>  
          clothes>   
    clothesList>  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    4.6.2 实例2

    import java.io.File;  
    import java.util.List;   
    import org.dom4j.Attribute;  
    import org.dom4j.Document;  
    import org.dom4j.DocumentException;  
    import org.dom4j.Element;  
    import org.dom4j.io.SAXReader;    
    public class Test {    
       public static void main(String[] args) {  
            String path = Test.class.getClassLoader().getResource("demo1.xml").getPath();  
    //      System.out.println(path);//获取文件路径  
            //读XML  
            //获取SAXReader解析器  
           SAXReader sax = new SAXReader();  
            //读取文件  
            try {  
                Document document = sax.read(new File(path));  
    //          System.out.println(document.getNodeTypeName());  
    //          System.out.println(document.getNodeType());  
                Element root = document.getRootElement();  
    //          System.out.println(root.getNodeTypeName());  
    //          System.out.println(root.getNodeType());  
    //          Element clo1 = root.element("clothes");//获取指定名称的元素,如果有重名,则获取第一个  
    //          List clos = root.elements();//获取所有的子元素,不包括孙子,只是儿子  
                List<Element> clos = root.elements("clothes");//获取所有指定名称的子元素  
                  
               for(Element clo : clos){  
    //              Attribute idAtt = clo.attribute("id");  
    //              System.out.println(idAtt.getNodeTypeName());  
    //              System.out.println(idAtt.getNodeType());  
    //              System.out.println(idAtt.getStringValue());  
    //              Attribute typeAtt = clo.attribute("type");  
                    String id = clo.attributeValue("id");  
                    String type = clo.attributeValue("type");  
                    System.out.println("id:"+id + ",type:"+type);                   
                    List<Element> hs = clo.elements("height");  
                    for(Element h : hs){                      				
                    System.out.println("name:"+h.attributeValue("name")+",value:"+h.attributeValue("value"));  
                   }  
                    Element exp = clo.element("explain");  
    //              System.out.println(exp.getText());  
    //              System.out.println(exp.getTextTrim());  
                    System.out.println(clo.elementTextTrim("explain"));;  
                }             
            } catch (DocumentException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    4.6.3 实例3

    package com.test;    
    import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.UnsupportedEncodingException;  
    import java.util.List;    
    import org.dom4j.Attribute;  
    import org.dom4j.Document;  
    import org.dom4j.DocumentException;  
    import org.dom4j.DocumentHelper;  
    import org.dom4j.Element;  
    import org.dom4j.io.OutputFormat;  
    import org.dom4j.io.SAXReader;  
    import org.dom4j.io.XMLWriter;   
    public class Test2 {  
      
        public static void main(String[] args) throws Exception {  
            //覆盖  
           String path = Test2.class.getClassLoader().getResource("").getPath();  
            File f = new File(path,"demo2.xml");  
            if(!f.exists()){  
               try {  
                    f.createNewFile();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            Document document = DocumentHelper.createDocument();  
            Element e = DocumentHelper.createElement("ele");//根节点  
           document.add(e);  
              
            Element e1 = DocumentHelper.createElement("ele1");  
            e.add(e1);  
            Attribute att1 = DocumentHelper.createAttribute(e1, "id", "1");  
            e1.add(att1);  
            e1.addCDATA("test1");  
               
            Element e2 = DocumentHelper.createElement("ele2");  
            e.add(e2);  
            Attribute att2 = DocumentHelper.createAttribute(e2, "id", "2");  
            e2.add(att2);  
            e2.addCDATA("test2");  
      
            OutputFormat format = OutputFormat.createPrettyPrint();   
            format.setEncoding("UTF-8");  
            XMLWriter writer = new XMLWriter(new FileOutputStream(f), format);  
            writer.write(document);  
            writer.close();       
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    4.6.4 实例4

    package com.test;    
    import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.UnsupportedEncodingException;  
    import java.util.List;  
    import org.dom4j.Attribute;  
    import org.dom4j.Document;  
    import org.dom4j.DocumentException;  
    import org.dom4j.DocumentHelper;  
    import org.dom4j.Element;  
    import org.dom4j.io.OutputFormat;  
    import org.dom4j.io.SAXReader;  
    import org.dom4j.io.XMLWriter;  
      
    public class Test3 {  
        public static void main(String[] args) throws Exception {  
            //改--->先读再改  
            String path = Test3.class.getClassLoader().getResource("demo2.xml").getPath();  
            File f = new File(path);          
            SAXReader r = new SAXReader();  
            Document d = r.read(f);  
            Element root = d.getRootElement();    
            List<Element> eles = root.elements();  
            Element e3 = DocumentHelper.createElement("ele3");  
            Attribute att1 = DocumentHelper.createAttribute(e3, "id", "3");  
            e3.add(att1);  
            e3.addCDATA("test3");     
            eles.add(e3);//添加到原本的集合当中去  
            OutputFormat format = OutputFormat.createPrettyPrint();   
            format.setEncoding("UTF-8");  
            XMLWriter writer = new XMLWriter(new FileOutputStream(f), format);  
            writer.write(d);  
            writer.close();       
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    5 注意事项

    项目启动后,第一件事情,是将XML当中的数据读到集合当中,集合就相当于数据库的二级缓存,单反修改二级缓存中的数据,都会触发对XML的写操作。

  • 相关阅读:
    【Debug危机系列】Embedding层的千层套路
    Go常见的语法题目
    get与post区别
    损失函数总结(五):PoissonNLLLoss、GaussianNLLLoss
    Java -- 每日一问:并发包中的 ConcurrentLinkedQueue 和 LinkedBlockingQueue 有什么区别?
    不为人知的暴利项目,互联网“抖音小店无货源”听说过的已经月入过万了
    产品补丁包测试的基本流程
    【04】基础知识:React组件实例三大核心属性 - state
    Abaqus多孔材料、多孔介质、双相材料、随机三维多孔结构建模插件:Random Porous Structure 3D
    实践DDD模拟电商系统总结
  • 原文地址:https://blog.csdn.net/u013916029/article/details/126258158