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 ↩︎