XML parsing []

1. Three ways to read the arrangement and configuration files in Java

A method
which is required to read out the code
Here Insert Picture Description
after reading the [] This method is not used
Here Insert Picture Description

1.1 XML and * .properties (properties file)
storage location: it is in the root directory of src

 Xxx.class.getResourceAsStream("/config.properties");

Reads the configuration file and classes in the same package

Xxx.class.getResourceAsStream("config2.properties");
package parse;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 各个位置下的资源文件(properties、xml结尾的文件)的读取方式
 * 1、同包下的资源文件读取
 * 2、将所有的资源文件放在根目录下
 * 		编译后所有的资源文件都会自动进WEB-INF下【一般开发用这种】
 * 3、直接将资源文件放到web-inf下
 * @author zhouyanpeng
 * 第一种方法 很少用
 */
public class PropertiesDemo {
	public static void main(String[] args) throws IOException {
		//将当前类同包下的db.properties资源文件转换成输入流
		//斜杠代表根目录
		InputStream in = PropertiesDemo.class.getResourceAsStream("/db.properties");
		Properties p = new Properties();
		p.load(in);
		//未获取到则返回null
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
	}
}

WEB-INF (or subdirectory)

ServletContext application = this.getServletContext();
InputStream is = application.getResourceAsStream("/WEB-INF/config3.properties");

This approach must be written Servlet and configure the job

public class PropertiesServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletContext context = req.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"));
	}
}

Configuration
Here Insert Picture Description

tomcat server folder meaning

//bin 放命令的
//config	放tomcat服务配置的
//work	临时文件
//webapp	发布项目的目录
//web-inf 安全目录	含义:不能被外界访问
//log	日志

Resource files
all at propertiesj or ending xml [xml resource files are also profiles], or all files are static resource files.
Common resource files
.properties and .xml
static resource files
css, js, html, images, videos
analytically properties files have java.util.properties this class to complete the
xml file: There should be a tool class to complete
the JDK / JDOM
SAX parsed by parsing down the
dom4j resolved from outside to inside parsing
dom node

<html>
	<body>
		<div id="head"></div>
		<div id="content"><
			xxxx
			<div></div>
		/div>
	</body>
</html>
<http></http><body></body><div></div>这些就是节点了

2. XML's role

Data transmission communication between the data transmission communication procedures between the program
PHP language <--100 <Express - Java language
configuration file config.xml configuration file
to store data, serves as a small database for storing data, it acts as a small database
specification data format standardized data format, having structural data, easier to read and process data having structural, easy to read and process

3. dom4j + xpath parse xml file

xpath equivalent select statement to the database

document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个

xml as data transmission
xml node and each node interface transfer content are not the same, then the corresponding resolution method should not write a lot of it?
Consideration a variety of analytical methods Analytical xml file formats
in reverse, and the set of check out any type of database is converted to the corresponding xml format string.
DOM consists of nodes
Node
element node
attribute nodes
text node

xpath
/ locate the path to build a file called document / students / student / sid in the system | name
@ attribute
students.xml

Wang acquiring
Here Insert Picture Description
method

public class XmlParseDemo {
	public static void main(String[] args) throws DocumentException {
		InputStream in = XmlParseDemo.class.getResourceAsStream("students.xml");
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(in);
//		System.out.println(doc.asXML());
//		1.获取到所有的学生
//		List<Element> stuEles = doc.selectNodes("/students/student");
//		2.遍历
//		for (Element stuEle : stuEles) {
//			if("s003".equals(stuEle.attributeValue("sid"))) {
//				Element nameEle = (Element)stuEle.selectSingleNode("name");
//				System.out.println(nameEle.asXML());
//				System.out.println(nameEle.getText());
//			}
//		}
//		3.做判断、如果sid=s003那么拿到小王
		Element stuEleS003 = (Element)doc.selectSingleNode("students/student[@sid='s003']");
		System.out.println(stuEleS003.selectSingleNode("name").getText());
	}
}

Method Two

InputStream in = XmlParseDemo.class.getResourceAsStream("students.xml");
	SAXReader saxReader = new SAXReader();
	Document doc = saxReader.read(in);
	Element stuEleS003 = (Element)doc.selectSingleNode("students/student[@sid='s003']");
	System.out.println(stuEleS003.selectSingleNode("name").getText());

This is relatively simple, but clearly know the location of the file
you want to learn more, contact the blogger.

Guess you like

Origin blog.csdn.net/zyp_baoku/article/details/90573376