• xml文件全面解析。


    1.什么是xml文件???

    xml文件和html文件一样,实际上是一个文本文件。它是一种可扩展标记语言,即简单的数据存储语言。使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然可扩展标记语言占用的空间比二进制数据要占用更多的空间,但可扩展标记语言极其简单易于掌握和使用。XML的简单使其易于在任何应用程序中读写数据,这使XML很快成为数据交换的公共语言。

    2. xml的作用?

    1.数据交互。

    2.配置文件。

    3.标准的xml格式。

     1.有且只有一个跟元素

    2.xml标签大小写正确区分

    3.正确使用结束标签

    4.正确嵌套标签

    5.使用了合法的标签名

    4. xml元素定义

    在xml加入DTD声明——<!DOCTYPE root[]>

    元素的分类——:

            <!ELEMENT element-name EMPTY>//空元素

            <!ELEMENT element-name (#PCDATA)>//文本元素

            <!ELEMENT element-name(e1,e2)>//混合元素

    元素的限制——:

            与(,)非(|)

            次数——:0或1:?

                                0-N:      *

                                 1-N:     +

    5.属性定义

    语法:<!ATTLIST element-name att_name type desc>

    属性类型type:ID.,(男|女),CDATA,IDREF,reference

    属性描述:#REQUIRED:必填

                        #IMPLIED:非必填

                        ‘默认值’(只有type为(男|女)类型时,desc才可以用默认值的方式)

    示例(xml元素和属性定义):

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE persons [
    3. <!ELEMENT persons (person+)>
    4. <!ELEMENT person (name,age,contact,br*)>
    5. <!ELEMENT name (#PCDATA)>
    6. <!ELEMENT age (#PCDATA)>
    7. <!ELEMENT contact (phone|email)>
    8. <!ELEMENT br EMPTY>
    9. <!ATTLIST person
    10. pid ID #REQUIRED
    11. sex (男|女) '男'
    12. qq CDATA #IMPLIED
    13. parent IDREF #IMPLIED
    14. >
    15. ]>
    16. <persons>
    17. <person pid="p1" sex="男" qq="aaa" parent="p2">
    18. <name>张小明</name>
    19. <age>10</age>
    20. <contact>
    21. <phone>1234567</phone>
    22. </contact>
    23. <br/>
    24. </person>
    25. <person pid="p2">
    26. <name>张大明</name>
    27. <age>35</age>
    28. <contact>
    29. <email>123@qq.com</email>
    30. </contact>
    31. </person>
    32. </persons>

    6.读取xml。

    1.定义一个xml文件:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!--
    3. config标签:可以包含0~N个action标签
    4. -->
    5. <!DOCTYPE config [
    6. <!ELEMENT config (action+)>
    7. <!ELEMENT action (forward+)>
    8. <!ATTLIST action
    9. path CDATA #REQUIRED
    10. type CDATA #REQUIRED
    11. >
    12. <!ATTLIST forward
    13. name CDATA #REQUIRED
    14. path CDATA #REQUIRED
    15. redirect (true|false) 'false'
    16. >
    17. ]>
    18. <config>
    19. <!--
    20. action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 ,子控制器对应的路径
    21. type:字符串,非空,子控制器的完整类名
    22. -->
    23. <action path="/registerAction" type="test.action.RegisterAction">
    24. <forward name="success" path="/index.jsp" redirect="true" />
    25. <forward name="failed" path="/register.jsp" redirect="false" />
    26. </action>
    27. <action path="/loginAction" type="test.action.LoginAction">
    28. <forward name="a" path="/index.jsp" redirect="false" />
    29. <forward name="b" path="/welcome.jsp" redirect="true" />
    30. </action>
    31. </config>

    先创建一个动态网页项目,点击src右击,点击new,点击source folder建一个文件夹,将xml文件放入其中。

    定义一个Java文件用来读取xml。

    1. package com.zking.demo;
    2. import java.io.InputStream;
    3. import java.util.List;
    4. import org.dom4j.Document;
    5. import org.dom4j.Element;
    6. import org.dom4j.io.SAXReader;
    7. public class XMLDemo {
    8. public static void main(String[] args) throws Exception {
    9. InputStream is = XMLDemo.class.getResourceAsStream("/config.xml");
    10. SAXReader reader = new SAXReader();
    11. Document doc = reader.read(is);
    12. Element element = doc.getRootElement();
    13. List<Element> list = element.selectNodes("/config/action");
    14. for (Element e1 : list) {
    15. String path = e1.attributeValue("path");
    16. String type = e1.attributeValue("type");
    17. System.out.println(path+"--------"+type);
    18. List<Element> list2 = e1.selectNodes("forward");
    19. for (Element e2 : list2) {
    20. String name2 = e2.attributeValue("name");
    21. String path2 = e2.attributeValue("path");
    22. String redirect2 = e2.attributeValue("redirect");
    23. System.out.println(name2+"------"+path2+"------"+redirect2);
    24. }
    25. }
    26. }
    27. }

    结果就是:

     当然需要一些读取xml文件的jar包。

    7.xml建模

    1.把xml通过java文件的方式处理到内存里面。

    2.一个标签就是一个对象,也就对应一些属性。

    上面xml文件Java对象就有config,action,forward三个,也对于一些属性和方法。

    如下:

    config类:

    1. package com.zking.mymvc.XMLMedle;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import com.zking.mymvc.XMLException.ActionNotFindException;
    5. import com.zking.mymvc.XMLException.ActionNotRepeatException;
    6. public class Config {
    7. private Map<String,Action> map = new HashMap<>();
    8. public void put(Action action) {
    9. if(map.containsKey(action.getPath())) {
    10. throw new ActionNotRepeatException("path:"+action.getPath()+"不能重复!");
    11. }
    12. map.put(action.getPath(), action);
    13. }
    14. public Action find(String path) {
    15. char charAt = path.charAt(0);
    16. if(charAt!='/') {
    17. throw new RuntimeException("path:"+path+"没有以 / 开头!");
    18. }
    19. if(!map.containsKey(path)) {
    20. throw new ActionNotFindException("path:"+path+"没有找到!");
    21. }
    22. return map.get(path);
    23. }
    24. }

    action类:

    1. package com.zking.mymvc.XMLMedle;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import com.zking.mymvc.XMLException.ForwardNotFindException;
    5. import com.zking.mymvc.XMLException.ForwardRepeatException;
    6. public class Action {
    7. private Map<String,Forward> map=new HashMap<>();
    8. private String path;
    9. private String type;
    10. public String getPath() {
    11. return path;
    12. }
    13. public void setPath(String path) {
    14. if(path.charAt(0)!='/') {
    15. throw new RuntimeException("path:"+path+"没有以 / 开头!");
    16. }
    17. this.path = path;
    18. }
    19. public String getType() {
    20. return type;
    21. }
    22. public void setType(String type) {
    23. this.type = type;
    24. }
    25. public void put(Forward forward) {
    26. if(map.containsKey(forward.getName())) {
    27. throw new ForwardRepeatException("name:"+forward.getName()+"不能重复!");
    28. }
    29. map.put(forward.getName(), forward);
    30. }
    31. public Forward find(String name) {
    32. if(!map.containsKey(name)) {
    33. throw new ForwardNotFindException("name:"+name+"没有找到!");
    34. }
    35. return map.get(name);
    36. }
    37. @Override
    38. public String toString() {
    39. return "Action [path=" + path + ", type=" + type + "]";
    40. }
    41. }

    forward类:

    1. package com.zking.mymvc.XMLMedle;
    2. public class Forward {
    3. private String name;
    4. private String path;
    5. private boolean redirect;
    6. public String getName() {
    7. return name;
    8. }
    9. public void setName(String name) {
    10. this.name = name;
    11. }
    12. public String getPath() {
    13. return path;
    14. }
    15. public void setPath(String path) {
    16. this.path = path;
    17. }
    18. public boolean isRedirect() {
    19. return redirect;
    20. }
    21. public void setRedirect(boolean redirect) {
    22. this.redirect = redirect;
    23. }
    24. public void setRedirect(String redirect) {
    25. if(redirect.equals("true") || redirect.equals("false")) {
    26. this.redirect = Boolean.valueOf(redirect);
    27. }else {
    28. throw new RuntimeException("redirect:"+redirect+"不是true和false");
    29. }
    30. }
    31. @Override
    32. public String toString() {
    33. return "Forward [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
    34. }
    35. }

    也对应一个简单工厂模式:

    1. package com.zking.mymvc.XMLMedle;
    2. import java.io.InputStream;
    3. import java.util.List;
    4. import org.dom4j.Document;
    5. import org.dom4j.Element;
    6. import org.dom4j.io.SAXReader;
    7. public final class XMLModelFactory {
    8. private XMLModelFactory() {
    9. }
    10. private static Config config=new Config();
    11. static {
    12. try {
    13. InputStream is = XMLModelFactory.class.getResourceAsStream("/config.xml");
    14. SAXReader reader = new SAXReader();
    15. Document doc = reader.read(is);
    16. Element element = doc.getRootElement();
    17. List<Element> list = element.selectNodes("/config/action");
    18. for (Element e1 : list) {
    19. String path = e1.attributeValue("path");
    20. String type = e1.attributeValue("type");
    21. Action action=new Action();
    22. action.setPath(path);
    23. action.setType(type);
    24. List<Element> list2 = e1.selectNodes("forward");
    25. for (Element e2 : list2) {
    26. String name2 = e2.attributeValue("name");
    27. String path2 = e2.attributeValue("path");
    28. String redirect2 = e2.attributeValue("redirect");
    29. Forward f=new Forward();
    30. f.setName(name2);
    31. f.setPath(path2);
    32. f.setRedirect(redirect2);
    33. action.put(f);
    34. }
    35. config.put(action);
    36. }
    37. }catch(Exception e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. public static Config getConfig() {
    42. return config;
    43. }
    44. public static void main(String[] args) {
    45. Config config2 = XMLModelFactory.getConfig();
    46. Action action = config2.find("/registerAction");
    47. System.out.println(action);
    48. Forward forward = action.find("success");
    49. System.out.println(forward);
    50. }
    51. }

    这样就把xml读取到内存上面了。

  • 相关阅读:
    SSL发送邮件时如何配置客户端确保安全性?
    C#上位机序列9: 批量读写+事件广播+数据类型处理
    探索Redis速度之谜
    这几种母乳才不能吃,“隔夜”的母乳不能吃?那是对母乳不够了解
    企业级开源版本管理系统GIT分析
    LeetCode:第302场周赛【总结】
    JAVA基础总结【面试】
    关于TCP通讯服务器断开连接时的问题!
    Centos7下安装Oracle11g
    双软认证的政策解读、好处及申报指南
  • 原文地址:https://blog.csdn.net/m0_64719055/article/details/125563914