MEC@JavaSE@进阶篇@笔记09@XML文件以及Properties文件解析初步

 一、XML文件

1、介绍

简介:

XML,即,Extensible Markup Language,中文翻译:可扩展标记语言是一种用于标记电子文件使其具有结构性的标记语言。

标签

标签是为了能表达有一定意义的数据的起、止标记,分起始标签和结束标签,就像我们在C或Java中经常见到的{}。起止标签之间的文本称之为内容。基本格式如下:
<student></student>

 XML标签遵循如下基本原则:

1、大小写敏感;
2、起止标签必须成对出现,且必须保持严格的顺序。

 正确的例子:

<student>

<id>20201025<id/>

</student>

错误的例子

<student>

<id>20201025</student>

<id/>

标签属性

在开始标签中,可以通过【键 = “值”】的形式定义一个或多个属性

上例可以改写成:

<student id = "20201025">张小三</student>

其中,id = "20201025 就是student标签的id属性。要注意的是: "="后面的值必须要用英文的""包裹起来。

XML文档头

XMl文件应当以一个文档头开始,如:

<?xml verson="1.0"?>

虽然文档头不是必须要写的,但是,强烈建议使用文档头

根标签

XML文件里的众多标签,都应该在一个根标签中书写。如:

<students>

       <student id = "20201025">张小三</student>

       <student id = "20201026">张小四</student>

</students>

注释

XML中的注释使用:

<!--  注释文字 -->(没有写错,就是这样的)

快捷键:Ctrl + /

注解在解析XML文件时,会自动忽略其中内容。

2、XML文档示例:

创建XML文档:

 

填写练习示例信息

格式一:着重使用标签

<?xml version="1.0" encoding="UTF-8"?>

<students>
    <student>
        <id>03167038</id>
        <name>张小三</name>
        <sex>男</sex>
        <birth>2000/5/31</birth>
        <hobbies>
            <hobby>篮球</hobby>
            <hobby>跑步</hobby>
            <hobby>足球</hobby>
        </hobbies>
        <introduce>
            张三之子,力大如牛,胆小如鼠
        </introduce>
    </student>
    <student>
        <id>03167039</id>
        <name>刘诗雅</name>
        <sex>女</sex>
        <birth>2000/10/12</birth>
        <hobbies>
            <hobby>唱歌</hobby>
        </hobbies>
        <introduce>
            刘家独女,热爱唱歌;五音已有其四,邻人不堪其扰,谓之刘嘶哑。
        </introduce>
    </student>
    <student>
        <id>03167066</id>
        <name>铁哥达</name>
        <sex>男</sex>
        <birth>2000/1/1</birth>
        <hobbies>
            <hobby>排球</hobby>
            <hobby>游泳</hobby>
            <hobby>蹦极</hobby>
        </hobbies>
        <introduce>
            作家之子:身板结实,称之铁疙瘩。
        </introduce>
    </student>
</students>

格式二:综合属性和标签

<?xml version="1.0" encoding="UTF-8"?>

<students>
    <student id = "03167038" name = "张小三" sex = "男" birth = "2000/5/31">
        <hobbies>
            <hobby>篮球</hobby>
            <hobby>跑步</hobby>
            <hobby>足球</hobby>
        </hobbies>
        <introduce>
            张三之子,力大如牛,胆小如鼠
        </introduce>
    </student>
    <student id = "03167039" name = "刘诗雅" sex = "女" birth = "2000/10/12">
        <hobbies>
            <hobby>唱歌</hobby>
        </hobbies>
        <introduce>
            刘家独女,热爱唱歌;五音已有其四,邻人不堪其扰,谓之刘嘶哑。
        </introduce>
    </student>
    <student id = "03167066" name = "铁哥达" sex = "男" birth = "2000/1/1">
        <hobbies>
            <hobby>排球</hobby>
            <hobby>游泳</hobby>
            <hobby>蹦极</hobby>
        </hobbies>
        <introduce>
            作家之子:身板结实,称之铁疙瘩。
        </introduce>
    </student>
</students>

3、解析XMl文档

通过程序设计的方式从XML文档中取出特定的标签的内容,或属性值,并将这些值转换成相应的类对象。

XMl解析器与W3C

Java提供专门的XML解析器,而且解析器也非一家。这里重点介绍W3C标准化了的DOM解析器。

DOM(Document Object Model):文件对象模型,是一种“树形解析器”。(XML文件中的标签与标签之间存在这明显的“一对多关系”,而一对多关系形成的就是:“树形”结构)。

解析代码示例:

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Test {
    public static void main(String[] args) {

        try {
            //创建工程类对象,在工厂类对象中创建应用类对象
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            //当前表明的是当前项目的src目录之下的位置
            //"/students.xml",表示的是绝对路径
            //InputStream is = Class.class.getClassLoader().getResourceAsStream("/students3.xml");
            //上面的示例没搞出来,改变方式,创建了一个新的字符流来接收
            //用InputStream(输入流)的形式,打开student.xml文档
            //当前路径指的是从项目名出发到src包下的students.xml文件“csdn_进阶篇/src”
            InputStream is = new FileInputStream(new File("csdn_进阶篇/src/students.xml"));
            //测试一下students.xml文档是否能打开
            System.out.println(is);

            //这里Document类一定要是org.w3c.dom.Document,绝对不能是其他包路径
            Document document = db.parse(is);
            //TagName的意思是“标签名称”,Tag:标签
            //getElementsByTagName的意思很明确:
            //取得指定标签名称的标签内容,以“节点列表”的形式
            //返回student.xml中的多个student标签节点内容。
            NodeList studentList = document.getElementsByTagName("student");
            //遍历这个节点列表
            //每一个节点就是student.xml文件中的
            //<student></student>标签的所有属性和内容
            for (int i = 0; i < studentList.getLength(); i++) {
                //这里Element类一定要是org.w3c.dom.Element,绝对不能是其他包路径
                Element student = (Element) studentList.item(i);
                //取得student标签的id属性
                String id = student.getAttribute("id");
                System.out.println("学号:" + id);
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

————————————————————————————————————————————————————————————————————————————————————————
java.io.FileInputStream@5fa7e7ff
学号:03167038
学号:03167039
学号:03167066

students3.xml文件中,每个学生信息都存在<hobby></hobby>标签,但是,他们分别属于不同的学生,即分属于不同的<student></student>标签。如果忽略了这个层次关系,直接从Document中提取<hobby></hobby>的数据,也是可以的,但是,结果肯定不是我们所期望的:

执行演示结果:

从students.xml文件中取出所有hobby标签内容:

            NodeList hobbyList = document.getElementsByTagName("hobby");
            for (int i = 0; i < hobbyList.getLength(); i++) {
                Element student = (Element) hobbyList.item(i);
                //获取<hobby></hobby>标签的内容
                String hobby = student.getTextContent();
                System.out.println("hobby:" + hobby);
            }
——————————————————————————————————————————————————————————————————————————————
hobby:篮球
hobby:跑步
hobby:足球
hobby:唱歌
hobby:排球
hobby:游泳
hobby:蹦极

从students.xml文件中取出所有hobbies标签内容:

            NodeList hobbiesList = document.getElementsByTagName("hobbies");
            for (int i = 0; i < hobbiesList.getLength(); i++) {
                Element student = (Element) hobbiesList.item(i);
                //获取<hobbies></hobbies>标签的内容
                String hobbies = student.getTextContent();
                System.out.println("hobbies:" + hobbies);
            }

——————————————————————————————————————————————————————————————————————————————————————
hobbies:
            篮球
            跑步
            足球
        
hobbies:
            唱歌
        
hobbies:
            排球
            游泳
            蹦极
        

上面的两种输出情况都是所有学生的爱好信息,但是这样的结果不是我们所需的。我们希望得到的是一个学生的爱好信息,而不是所有。

得到一个学生的所有爱好信息代码示例:

            NodeList studentList = document.getElementsByTagName("student");
            //遍历这个节点列表
            //每一个节点就是student.xml文件中的
            //<student></student>标签的所有属性和内容
            for (int i = 0; i < studentList.getLength(); i++) {
                Element student = (Element) studentList.item(i);
                //取得student标签的id属性
                String id = student.getAttribute("id");
                //取得student标签的name属性
                String name = student.getAttribute("name");
                System.out.println(id +": " + name);
                //以Element的student对象进行为基础,创建hobbyList对象
                NodeList hobbyList = student.getElementsByTagName("hobby");
                for (int j = 0; j < hobbyList.getLength(); j++) {
                    Element hobbyTag = (Element) hobbyList.item(j);
                    //获取<hobby></hobby>标签的内容
                    String hobby = hobbyTag.getTextContent();
                    System.out.println("hobby:" + hobby);
                }
            }

————————————————————————————————————————————————————————————————————————————————————————
03167038: 张小三
hobby:篮球
hobby:跑步
hobby:足球
03167039: 刘诗雅
hobby:唱歌
03167066: 铁哥达
hobby:排球
hobby:游泳
hobby:蹦极

二、Properties文件解析

Properties文件解析相对较容易,直接用键值对就行。

Properties文件示例:

# 最大值
maxNUM = 30
# 最小值
minNUm = 20
# 增量
incNUM = 3

解析代码示例:

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;

public class TestProperties {

    public static void main(String[] args) {

        try {
            Properties properties = new Properties();
            InputStream is = new FileInputStream(new File("csdn_进阶篇/src/config.properties"));
            System.out.println(is);
            properties.load(is);

            Enumeration<Object> keys = properties.keys();
            while (keys.hasMoreElements()){
                String key = (String) keys.nextElement();
                String value = properties.getProperty(key);
                System.out.println(key + ":" + value);
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //测试一下students.xml文档是否能打开
    }
}

——————————————————————————————————————————————————————————————————————————————————
java.io.FileInputStream@10f87f48
incNUM:3
maxNUM:30
minNUm:20

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值