Learning log --2019 / 08/30

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44757417/article/details/100165354

XML

CDATA area

invalid symbol
<?xml version="1.0" encoding="UTF-8"?>
<a>
	<!--这是俩个学生一个叫张三,一个叫李四 -->
	<xs id="1">
		<name>李四</name>
		<age>19</age>
	</xs>
	<xs>
		<name id="2">张三< </name>
		<age>20</age>
	</xs>
</a>
  • "<" Character is being given illegal

Strictly speaking, the only character in XML "<" and "&" are illegal. Ellipsis, quotation marks and greater than is legal, but they replaced entity reference is a good habit.

 <   &lt;
 &   &amp;

If there are too many of a certain string of characters, and which contains the text of this label or similar keywords, you do not want to parse the xml parser. You can use CDATA to!

<des>
<![CDATA[<a href="http://www.baidu.com"> 百度</a>]]>
</des>

But this is generally less CDATA see. Usually end server returns data to the client time.

XML parsing

In fact, the element is to get inside the character data or attribute data.

  • There are a variety of XML parsing mode, but there are usually two
    • JUDGMENT
    • SAX
      Here Insert Picture Description
API for the two analytical methods

Some organizations or companies, for more than two analytical way, given what the solution?

	jaxp  sun公司。 比较繁琐

	jdom
	dom4j  使用比较广泛
  • Dom4j basic usage

    element.element ( "stu"): Returns the first element in the element stu
    element.elements (); return all child elements of the element.

  1. Objects created SaxReader

  2. Specifies the xml parsing

  3. Gets the root element.

  4. Get the following sub-element or elements based on the root element descendants


		try {
			//1. 创建sax读取对象
			SAXReader reader = new SAXReader(); //jdbc -- classloader
			//2. 指定解析的xml源
			Document  document  = reader.read(new File("src/xml/stus.xml"));
			
			//3. 得到元素、
			//得到根元素
			Element rootElement= document.getRootElement();
			
			//获取根元素下面的子元素 age
		//rootElement.element("age") 
			//System.out.println(rootElement.element("stu").element("age").getText());


			//获取根元素下面的所有子元素 。 stu元素
			List<Element> elements = rootElement.elements();
			//遍历所有的stu元素
			for (Element element : elements) {
				//获取stu元素下面的name元素
				String name = element.element("name").getText();
				String age = element.element("age").getText();
				String address = element.element("address").getText();
				System.out.println("name="+name+"==age+"+age+"==address="+address);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

Guess you like

Origin blog.csdn.net/weixin_44757417/article/details/100165354