175.XML解析

雲疏不知數發表於2020-10-18

XML1

XML:可擴充套件標記語言,作為資料的一種儲存格式或者用於儲存資料的引數,程式解析此配置檔案,就可以達到不修改程式碼就能更改程式功能的目的

SAX解析

熟悉使用流程:

  1. 獲取解析工廠

  2. 從解析工廠獲取解析器

  3. 載入文件Document註冊處理器,需要繼承DefaultHandler實現一個類

  4. 編寫處理器

程式碼演示
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/*
 * SAX流程
 */
public class XMLSAX {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		//SAX解析
		//1.獲取解析工廠
		SAXParserFactory factory = SAXParserFactory.newInstance();
		
		//2.從解析工廠獲取解析器
		SAXParser parse = factory.newSAXParser();
		
		//3.載入文件Document註冊處理器
		PersonHandler handler = new PersonHandler();
		
		//4.編寫處理器
		parse.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/server/basic/p.xml"), handler);
	}
}
class PersonHandler extends DefaultHandler{
	@Override
	public void startDocument() throws SAXException {
		System.out.println("解析開始");
	}
	
	@Override
	public void endDocument() throws SAXException {
		System.out.println("解析結束");
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String contents = new String(ch, start, length).trim(); //trim() 方法用於刪除字串的頭尾空白符
		if(contents.length()>0)
			System.out.println("內容:"+contents);
		else
			System.out.println("內容:空");
	}
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		System.out.println(qName + "-->start");
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println(qName + "-->end");
		
	}
}

注意:編寫處理器時xml的檔案路徑要指定對

上述程式碼使用的xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<persons>
  <person>
   <name>至尊寶</name>
   <age>9000</age>
  </person>
  <person>
   <name>白晶晶</name>
   <age>7000</age>
  </person>
</persons>

每個標籤中間的空格也會被解析出來

175.XML解析

DefaultHandler

DefaultHandler成員方法

Modifier and TypeDescription
void characters(char[] ch, int start, int length)Receive notification of character data inside an element.
void endDocument()Receive notification of the end of the document.
void endElement(String uri, String localName, String qName)Receive notification of the end of an element.
void endPrefixMapping(String prefix)Receive notification of the end of a Namespace mapping.
void error(SAXParseException e)Receive notification of a recoverable parser error.
void fatalError(SAXParseException e)Report a fatal XML parsing error.
void ignorableWhitespace(char[] ch, int start, int length)Receive notification of ignorable whitespace in element content.
void notationDecl(String name, String publicId, String systemId)Receive notification of a notation declaration.
void processingInstruction(String target, String data)Receive notification of a processing instruction.
InputSource resolveEntity(String publicId, String systemId)Resolve an external entity.
void setDocumentLocator(Locator locator)Receive a Locator object for document events.
void skippedEntity(String name)Receive notification of a skipped entity.
void startDocument()Receive notification of the beginning of the document.
void startElement(String uri, String localName, String qName, Attributes attributes)Receive notification of the start of an element.
void startPrefixMapping(String prefix, String uri)Receive notification of the start of a Namespace mapping.
void unparsedEntityDecl(String name, String publicId, String systemId, String notationName)Receive notification of an unparsed entity declaration.
void warning(SAXParseException e)Receive notification of a parser warning.

  1. Extensible Markup Language ↩︎