• XML解析


    读取指定文件下的资源文件(db.propeties)
    1、读取同包下的资源文件
    2、资源文件存放在根目录下

    properties.class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
     
    public class properties {
     
        public static void main(String[] args) throws IOException {
             //1、读取根目录下文件(db.propeties)
     
           InputStream in=properties.class.getResourceAsStream("/db.properties");
     
          //2、读取同包下文件(db.propeties)
        //InputStream in=properties.class.getResourceAsStream("db.properties");
          
     
            Properties p=new Properties();
            p.load(in);
            System.out.println(p.getProperty("uname"));
            System.out.println(p.getProperty("upass"));
         
        }
         
    }

      

    3、资源文件存放在web-inf文件(db.propeties)

    1
    parseServlet .java
    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
    52
    package com.zking.properties;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
     
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    /**
     * Servlet implementation class parseServlet
     */
    public class parseServlet extends HttpServlet {
         
        private static final long serialVersionUID = 1L;
            
        /**
         * @see HttpServlet#HttpServlet()
         */
        public parseServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
     
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);             
        }          
     
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
            ServletContext context=request.getServletContext();
             
            InputStream in=context.getResourceAsStream("/WEB-INF/db.properties");
             
            Properties p=new Properties();
            p.load(in);
            System.out.println(p.getProperty("uname"));
            System.out.println(p.getProperty("upass"));
     
         
        }
     
    }

      

    dom4j + xpase解析XML

    1
    XMLDemo .class
    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
    package com.zking.properties;
     
    import java.io.InputStream;
    import java.util.List;
     
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;
     
    /*
     * dom4j
     * jdom jdk
     * sax解析
     *
     * 解析指定路径下的支援
     *
     */
     
    public class XMLDemo {
    public static void main(String[] args) throws Exception {
           
        InputStream in= XMLDemo.class.getResourceAsStream("students.xml");
          
            SAXReader sax=new SAXReader();
            Document doc=sax.read(in);
            System.out.println(doc.asXML());
    //      xpath解析
    //      xpath解析能够将xml格式的串当作目录结构来进行查找
    //      List<Element> list=doc.selectNodes("/students/student");
    //      for (Element element : list) {
    //              if("s002".equals(element.attributeValue("sid"))) {
    //          System.out.println(element.asXML());
    //          Element name=(Element)element.selectSingleNode("name");
    //          System.out.println(name.asXML());
    //          System.out.println(name.getText());
    //         
    //              }
    //      }
             
            Element  node=(Element) doc.selectSingleNode("/students/student[@sid='s002']");
            System.out.println(node.asXML());
         
            }
         
        }

    xml解析

    Temp.class

    1、获取所有action中的type的值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
      public static void main(String[] args) throws Exception {
            InputStream in=XmlDemo.class.getResourceAsStream("config.xml");       
            SAXReader sax= new SAXReader();
            Document doc=sax.read(in);
      //       获取所有action中的type的值
            List<Element> stuEles= doc.selectNodes("/config/action");
            for (Element stuEle : stuEles) {
                String type=stuEle.attributeValue("type");
                System.out.println(type);
                     
            }   
    }

      


    2、获取第二个action中的type的值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public static void main(String[] args) throws Exception {
      List<Element> stuEles= doc.selectNodes("/config/action");
            for (Element stuEle : stuEles) {
                if("/loginAction".equals(stuEle.attributeValue("path"))) {
                     String type=stuEle.attributeValue("type");
                     System.out.println(type);
                   }
                }
    }

      


    3、获取第二个action的所有forward的path

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public static void main(String[] args) throws Exception {
        List<Element> stuEles= doc.selectNodes("/config/action");
            for (Element stuEle : stuEles) {
                if("/loginAction".equals(stuEle.attributeValue("path"))) {
                    List<Element> ford=(List<Element>) stuEle.selectNodes("forward");
                    for (Element element : ford) {
                       String path=element.attributeValue("path");
                       System.out.println(path);
                    }
                }
            }
    }

      


    4、获取第二个action的第二个forward的path

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public static void main(String[] args) throws Exception {
      List<Element> stuEles= doc.selectNodes("/config/action");
            for (Element stuEle : stuEles) {
                if("/loginAction".equals(stuEle.attributeValue("path"))) {
                   List<Element> ford=(List<Element>) stuEle.selectNodes("forward");
                    for (Element element : ford) {
                        if("success".equals(element.attributeValue("name"))) {
                        String path=element.attributeValue("path");
                        System.out.println(path);
                        }
                    }
                }
            }
     
     
        }
  • 相关阅读:
    Windows 2003/2008更改远程桌面端口脚本
    如何修改远程桌面连接3389端口
    关于百度地图(离线)使用过程报“Cannot read property 'jb' of undefined ”错误的解决办法
    IIS 错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    IIS 错误:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
    IIS7错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny")......
    Jqgrid pager 关于“local” dataType 动态加载数据分页的研究(没好用的研究结果)
    JQGrid导出Excel文件
    Oracle以15分钟为界,统计一天内各时间段的数据笔数
    ORA-01438: 值大于为此列指定的允许精度
  • 原文地址:https://www.cnblogs.com/BAYOUA/p/11004314.html
Copyright © 2020-2023  润新知