当前位置:网站首页>Parsing of XML files

Parsing of XML files

2022-07-07 14:04:00 Xiao afai_

This issue is wonderful

Catalog

Knowledge required

1、Java Three configuration locations and reading methods of configuration files in

2、dome4j Common methods

3、xpath grammar

analysis XML Code operation


Knowledge required

1、Java Three configuration locations and reading methods of configuration files in

Class name .class.getResourceAsStream("xxx"): Get the documents under the same package

Class name .class.getResourceAsStream("/xxx"): Get the file under the root directory

context.getResourceAsStream("/WIN-INF/xxx"): Get WIN-INF Safe path

2、dome4j Common methods

selectNodes: Get multiple elements

selectSingleNode: Get a single element

getRootElement(): Get the root element

attributeValue: Only elements can click this method to get values

getText: Get the element text

notes : Elements can be nodes , But the attribute in the element is a node, not an element

3、xpath grammar

/: Positioning path

@: attribute

analysis XML Code operation

1、 It needs to be parsed XML Case study (config.xml file )

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
	<!ELEMENT config (action*)>
	<!ELEMENT action (forward*)>
	<!ELEMENT forward EMPTY>
	<!ATTLIST action
	  path CDATA #REQUIRED
	  type CDATA #REQUIRED
	>
	<!ATTLIST forward
	  name CDATA #REQUIRED
	  path CDATA #REQUIRED
	  redirect (true|false) "false"
	>
]>
<config>
	<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
		<forward name="students" path="/students/studentList.jsp" redirect="false"/>
	</action>
	<action path="/studentAction02" type="org.lisen.mvc.action.StudentAction">
		<forward name="students02" path="/students/studentList.jsp" redirect="false"/>
	</action>
</config>

2、 Before writing parsing code, you need to import jar package

3、 Write the code of parsing operation

public class XmlReader {
	
	public static void main(String[] args) throws Exception {
		InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(in);

		Element rootElement = doc.getRootElement();
		List<Element> actions = rootElement.selectNodes("action");
		
		for(Element e:  actions) {
			String path = e.attributeValue("path");
			String type = e.attributeValue("type");
			
			System.out.println("action path = "+path);
			System.out.println("action type = "+type);
			
			List<Element> forwards = e.selectNodes("forward");
			for(Element f:  forwards) {
				String name = f.attributeValue("name");
				String fPath = f.attributeValue("path");
				String redirect = f.attributeValue("redirect");
				
				System.out.println("forward name = "+name);
				System.out.println("forward fPath = "+fPath);
				System.out.println("forward redirect = "+redirect);
			}
			System.out.println(" Parsing ended ");
		}
	}
}

Output results ( You can see that you have obtained the information provided above XML Relevant elements and nodes in the file )

原网站

版权声明
本文为[Xiao afai_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071158584364.html