拉钩:解析xml

Posted xjatj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了拉钩:解析xml相关的知识,希望对你有一定的参考价值。

技术图片

 

 技术图片

 技术图片

 

 

 

 1 public class XMLConfigBuilder {
 2 
 3     private Configuration configuration;
 4 
 5     public XMLConfigBuilder() {
 6         this.configuration = new Configuration();
 7     }
 8 
 9     /**
10      * 该方法就是使用dom4j对配置文件进行解析,封装Configuration
11      */
12     public Configuration parseConfig(InputStream inputStream) throws DocumentException, PropertyVetoException {
13 
14         Document document = new SAXReader().read(inputStream);
15         //<configuration>
16         Element rootElement = document.getRootElement();
17         List<Element> list = rootElement.selectNodes("//property");
18         Properties properties = new Properties();
19         for (Element element : list) {
20             String name = element.attributeValue("name");
21             String value = element.attributeValue("value");
22             properties.setProperty(name,value);
23         }
24 
25         ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
26         comboPooledDataSource.setDriverClass(properties.getProperty("driverClass"));
27         comboPooledDataSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
28         comboPooledDataSource.setUser(properties.getProperty("username"));
29         comboPooledDataSource.setPassword(properties.getProperty("password"));
30 
31         configuration.setDataSource(comboPooledDataSource);
32 
33         //mapper.xml解析: 拿到路径--字节输入流---dom4j进行解析
34         List<Element> mapperList = rootElement.selectNodes("//mapper");
35 
36         for (Element element : mapperList) {
37             String mapperPath = element.attributeValue("resource");
38             InputStream resourceAsSteam = Resources.getResourceAsSteam(mapperPath);
39             XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);
40             xmlMapperBuilder.parse(resourceAsSteam);
41         }
42         return configuration;
43     }
44 
45 
46 }
 1 package com.lagou.config;
 2 
 3 import com.lagou.pojo.Configuration;
 4 import com.lagou.pojo.MappedStatement;
 5 import org.dom4j.Document;
 6 import org.dom4j.DocumentException;
 7 import org.dom4j.Element;
 8 import org.dom4j.io.SAXReader;
 9 
10 import java.io.InputStream;
11 import java.util.List;
12 
13 public class XMLMapperBuilder {
14 
15     private Configuration configuration;
16 
17     public XMLMapperBuilder(Configuration configuration) {
18         this.configuration =configuration;
19     }
20 
21     public void parse(InputStream inputStream) throws DocumentException {
22 
23         Document document = new SAXReader().read(inputStream);
24         Element rootElement = document.getRootElement();
25 
26         String namespace = rootElement.attributeValue("namespace");
27 
28         List<Element> list = rootElement.selectNodes("//select");
29         for (Element element : list) {
30             String id = element.attributeValue("id");
31             String resultType = element.attributeValue("resultType");
32             String paramterType = element.attributeValue("paramterType");
33             String sqlText = element.getTextTrim();
34             MappedStatement mappedStatement = new MappedStatement();
35             mappedStatement.setId(id);
36             mappedStatement.setResultType(resultType);
37             mappedStatement.setParamterType(paramterType);
38             mappedStatement.setSql(sqlText);
39             String key = namespace+"."+id;
40             configuration.getMappedStatementMap().put(key,mappedStatement);
41 
42         }
43 
44     }
45 
46 
47 }

 

以上是关于拉钩:解析xml的主要内容,如果未能解决你的问题,请参考以下文章

拉钩网爬取所有python职位信息

从流输入中解析没有根元素的 XML 片段列表

无法解析片段中的 findViewById [重复]

jsoup解析xml某片段的问题

为 Blogger 上的博客格式化代码片段 [关闭]

java解析xml的几种方式哪种最好?