XML && Dom4J

XML
definition: eXtensible Markup Language: an extensible markup language
Function: as a configuration file, because it is a universal data exchange format (that is, data can be used in different languages)
Source: XML technology is W3C organization (World Wide Web Consortium) released, because release is perfect, so it has always been version 1.0

Writing details:
1. Eclipse quickly creates a certain file: Window—>Perspective—>Customize Perspective—>Shortcuts—> tick the corresponding file type.
2. The first line of the XML file: <?xml version="1.0" encoding=" UTF-8"?>
3. XML must have a unique root tag
4. Special characters must be escaped: &—&<—<>—>"—"'—&apos
5. Because there are special characters, So either escape or write it in the CDATA area (the parser will not parse the content inside)
<![CDATA[* <><><><><>&^*(^*(^&*(3sdfjpwrjp2024372394j* (*<> *))>

XML constraints:
the role of dtd and schema constraints: when writing, standardize xml tags (according to your own requirements), and you can have corresponding tips on the tool!
dtd
Insert picture description here
schema
Insert picture description here
The difference between dtd and schema:
dtd: no one restricts it, it is easy to write wrong, the function should be simpler
Schema: there are corresponding tags to restrict it, the function is more powerful, so it is more complicated

==============================================================

DOM : Document Object Model,
is to turn the xml file into a tree structure according to the tags, and each element is parsed into a Node object
Insert picture description here

=============================================================

Dom4J parses XML
1. Guide package: dom4j-1.6.1.jar
2. XPath : XML path language, which is a language used to determine the location of a certain part of an XML document. XPath is based on the XML tree structure, with different types of nodes, including element nodes, attribute nodes and text nodes, and provides the ability to find nodes in the data structure tree.
(That is: XPath is a language for finding information in XML documents, which is used to navigate through elements and attributes in XML documents.)
3. Note: When using XPath to simplify dom4j for node search, it depends on jaxen-1.1-beta -6.jar, remember to guide the package
4. Two kinds of analysis: obtain the lower-level label __or__ through the XPath specified location to obtain the
extended cold knowledge: the middle 4 is actually for, simple and understand~

public class ParseXmlTest {
    
    
	/**解析XML文件时,如果XML用了约束,但是程序找不到对应的约束文件,是会报错的*/
	File file = new File("dtd/contacts.xml");
	
	private Document getDoc() throws DocumentException {
    
    
		//1.别人写好的类和对象:解析器,Reader,字符输入流
		SAXReader reader = new SAXReader();
		//2.document代表了  文档
		Document document = reader.read(file);
		return document;
	}
	
	/**
	 * 读取xml数据
	 */
	@Test
	public void testName() throws Exception {
    
    
		//通过解析器字符流,将硬盘上的文件加载到内存上,返回Document对象
		Document document = getDoc();
        //3.获得根标签
        Element root = document.getRootElement();
        //4.通过父标签获得子标签
        Element linkman = root.element("linkman");
        Element name = linkman.element("name");
        //5.获取文本信息
        System.out.println(name.getText());
	}
	
	/**
	 * 通过xpath解析xml
	 */
	@Test
	public void testXpath() throws Exception {
    
    
		Document doc = getDoc();
		//XML中的所有对象都是Node对象
		//通过XPath获得指定节点对象
        Node node = doc.selectSingleNode("/contacts/linkman[1]/name");
        String text = node.getText();
        System.out.println(text);
	}
	
	/**
	*  其实增删改,就是先把xml读取到内存中,对DOM对象操作之后,再输出到硬盘上
	*/
	/**
	 * xml添加操作
	 */
	@Test
	public void testAdd() throws Exception {
    
    
		Document doc = getDoc()
        
        Element contacts = doc.getRootElement();
        //contacts下面添加了linkman  ,返回的就是linkman对象
        Element linkman = contacts.addElement("linkman");
        //linkman添加属性
        linkman.addAttribute("id", "3");
        
        linkman.addElement("name").setText("sss");
        linkman.addElement("email").setText("[email protected]");
        linkman.addElement("address").setText("北京");
        linkman.addElement("group").setText("仙女");
        
        //原生的格式:数据都在一行,丑但是节约空间
       /* XMLWriter writer = new XMLWriter(
                new FileWriter( file )
            );
        writer.write( doc );
        writer.close();*/
        
        prettyPrint(doc);
	}
	
	/**
	 * 更新操作
	 */
	@Test
	public void testUpdate() throws Exception {
    
    
		Document doc = getDoc();
       
        Node name = doc.selectSingleNode("/contacts/linkman[2]/name");
        name.setText("qq");
        
        prettyPrint(doc);
	}

	
	/**
	 * 删除操作
	 * remove方法只有Element接口有,Node接口没有,所以要强转
	 */
	@Test
	public void testRemove() throws Exception {
    
    
		Document doc = getDoc();
        
        Element linkman = (Element)doc.selectSingleNode("/contacts/linkman[2]");
        Node name = doc.selectSingleNode("/contacts/linkman[2]/name");
        //父节点移除子节点
        linkman.remove(name);
        
        //或者节点自己获取父节点,来移除自己
		//name.getParent().remove(name);
	
        prettyPrint(doc);
	}
	
	/**
	 * 将内存中的Document按照标准xml格式输出,不然就是一行,不好看
	 */
	private void prettyPrint(Document doc) throws IOException {
    
    
		//漂亮打印
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter( new FileWriter( file ), format );
		writer.write( doc );
		writer.close();
	}
}

Guess you like

Origin blog.csdn.net/ExceptionCoder/article/details/108291243