175.XML解析
XML1
XML:可擴充套件標記語言,作為資料的一種儲存格式或者用於儲存資料的引數,程式解析此配置檔案,就可以達到不修改程式碼就能更改程式功能的目的
SAX解析
熟悉使用流程:
-
獲取解析工廠
-
從解析工廠獲取解析器
-
載入文件Document註冊處理器,需要繼承DefaultHandler實現一個類
-
編寫處理器
程式碼演示
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>
每個標籤中間的空格也會被解析出來
DefaultHandler
DefaultHandler成員方法
Modifier and Type | Description |
---|---|
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. |
Extensible Markup Language ↩︎
相關文章
- 解析-解析器
- ORACLE SQL解析之硬解析和軟解析OracleSQL
- 解析-HTML 解析器HTML
- Nginx(六):配置解析之location解析Nginx
- XML 檔案解析實踐 (DOM 解析)XML
- dom解析和sax解析的區別
- 雲解析DNS如何實現智慧解析?DNS
- 什麼是智慧DNS雲解析?雲解析如何實現智慧解析效果?DNS
- 雲解析VS傳統解析,雲解析到底有哪些實際用途?
- quicklink解析UI
- ThreadLocal解析thread
- 解析 SQLiteOpenHelperSQLite
- LongAdder解析
- Handler解析
- xpath解析
- this 全面解析
- 容器解析
- 解析ThreadLocalthread
- OkHttp解析HTTP
- FileReader 解析
- this全面解析
- AQS解析AQS
- ThreadLocal 解析thread
- Go解析Go
- Semaphore解析
- Xml解析XML
- PointRend解析
- Handler訊息機制完全解析Handler解析
- 什麼是DNS雲解析?雲解析和普通解析有什麼區別?DNS
- 軟解析、硬解析的一個小測試
- 什麼是DNS解析?如何提升DNS解析安全?DNS
- this 全面解析(一)
- DNS解析流程DNS
- ConstraintLayout 全解析AI
- this 繫結解析
- Immer 全解析
- this全面解析(二)
- JavaScriptCore全面解析JavaScript