175.XML解析

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>

每个标签中间的空格也会被解析出来

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值