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
- ORACLE 硬解析和軟解析 軟軟解析Oracle
- 解析-HTML 解析器HTML
- DOM解析和SAX解析
- 軟解析和硬解析
- Oracle中的遊標、硬解析、軟解析、軟軟解析、解析失敗Oracle
- 徹底弄懂oracle硬解析、軟解析、軟軟解析Oracle
- Java解析Excel例項解析JavaExcel
- Oracle 硬解析與軟解析Oracle
- 解析
- Nginx(六):配置解析之location解析Nginx
- Oracle的硬解析和軟解析Oracle
- dom解析和sax解析的區別
- 雲解析DNS如何實現智慧解析?DNS
- XML 檔案解析實踐 (DOM 解析)XML
- Oracle SQL的硬解析和軟解析OracleSQL
- 草稿 - 遊標,硬解析,軟解析 等
- TS流解析之PMT表格解析(轉)
- TS流解析之PAT表格解析(轉)
- 使用JAXP進行DOM解析_SAX解析
- soft parse(軟解析),hard parse(硬解析)
- 解析 SQLiteOpenHelperSQLite
- this 全面解析
- OkHttp解析HTTP
- this全面解析
- Semaphore解析
- Xml解析XML
- Go解析Go
- 容器解析
- 解析ExcelExcel
- Handler解析
- HTML解析HTML
- pull解析
- 解析Ruby
- DOM 解析
- HTTP解析HTTP
- Intent 解析Intent