您的位置:首页 > 其它

XML解析之JAXP

2016-08-29 20:33 120 查看

XML解析方式

DOM

Document Object Model,文档对象模型。这种方式是W3C推荐的处理XML的一种方式。
Dom解析将XML文件全部读入并生成DOM树,所有的XML标签,属性和文本内容均为DOM树上的节点,并采用节点操作的方式可以对任意一个元素进行增删改查。

SAX

Simple API for XML。这种方式不是官方标准,属于开源社区XML-DEV,几乎所有的XML解析器都支持它。
SAX解析是基于事件驱动的XML解析方式,该方式的工作需要依赖事件处理接口,根据发生的事件的发生来针对性的采取对应操作。由于该方式产生操作不需要加载全部XML文件,所以对于超大的XML文件的解析效率较高。

XML解析开发包

JAXP:是SUN公司推出的解析标准实现。
Dom4J:是开源组织推出的一种解析开发包,较为常用。
JDom:是开源组织推出的一种解析开发包。


JAXP


概述

JAXP(Java API for XML Processing)开发包是JavaSE的一部分,它由以下几个包及其子包组成:
org.w3c.dom:提供DOM方式解析XML的标准接口。
org.xml.sax:提供SAX方式解析XML的标准接口。
javax.xml:提供了解析XML文档的类。
javax.xml.parsers:提供了对XML文档解析工厂类,通过工厂类(DocumentBuilderFactory、SAXParserFactory)可以得到对XML文档进行解析的DOM和SAX解析器对象。

使用DOM方式解析XML文档


1. 常用API

解析器工厂类:DocumentBuilderFactory
解析器:DocumentBuilder
文档对象:Document
节点对象:Node
节点集合:NodeList
元素:Element
修改文件工厂类:TransformerFactory
保存文件:Transformer


2. 解析XML获取文档对象

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pun" style="color: rgb(0, 0, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilderFactory</span><span class="pln" style="color: rgb(0, 0, 0);"> factory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilder</span><span class="pln" style="color: rgb(0, 0, 0);"> builder </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">try</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 先拿到解析器的工厂类</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    factory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilderFactory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newInstance</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 拿到解析器</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    builder </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> factory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newDocumentBuilder</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 3. 解析XML,获取文档对象</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    document </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> builder</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">parse</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"src/LocalList.xml"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">catch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">printStackTrace</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



3. 获取节点文本

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> getNodeText</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 先拿到元素名称为city的所有节点</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">NodeList</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"City"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 找到指定位置的节点</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">7</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 3. 打印文本内容</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Node Text is "</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>getTextContent</strong></span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>


注意:获取节点文本包括暗部和所有的子节点。


4. 递归节点遍历

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> getAllNodeText</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 获得需要遍历的节点</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"State"</span><span class="pun" style="color: rgb(0, 0, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 递归调用的开始</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  recursion</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">node</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> recursion</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 只有元素节点才打印</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getNodeType</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ELEMENT_NODE</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Node Name is "</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getTextContent</span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 拿到当前节点的所有子节点</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">NodeList</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>getChildNodes</strong></span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(0, 0, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getLength</span><span class="pun" style="color: rgb(0, 0, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> i</span><span class="pun" style="color: rgb(0, 0, 0);">++)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">i</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    recursion</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



5. 保存更改到文件

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> save</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 拿到保存文件对象的工厂类</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">TransformerFactory</span><span class="pln" style="color: rgb(0, 0, 0);"> tfactory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">TransformerFactory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newInstance</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 拿到保存文件的对象</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Transformer</span><span class="pln" style="color: rgb(0, 0, 0);"> tf </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> tfactory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newTransformer</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 3. 保存文件</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// DOMSource 用来指定 domcument 对象</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  tf</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>transform</strong></span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DOMSource</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">),</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">StreamResult</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"src/LocalList.xml"</span><span class="pun" style="color: rgb(0, 0, 0);">));</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



6. 修改节点文本内容

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> modifyNodeText</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 拿到street节点</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"street"</span><span class="pun" style="color: rgb(0, 0, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 设置文本内容</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>setTextContent</strong></span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"和平门大街"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 以上并没有真正的取保存文件,只是修改了内存中数据</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;">
<code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  save</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



7. 添加子节点

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> createChildNode</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 拿到要添加位置的父节点</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"City"</span><span class="pun" style="color: rgb(0, 0, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">2</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 创建一个子节点,节点名字叫street</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> e </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"street"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span><span class="com" style="color: rgb(63, 127, 95);">// 创建了<street></street></span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setTextContent</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"前门大街"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span><span class="com" style="color: rgb(63, 127, 95);">// <street>前门大街</street></span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 3. 添加到列表末尾</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>appendChild</strong></span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  save</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



8. 添加兄弟节点

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> createNode</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 拿到要添加的指定位置的后一个</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"City"</span><span class="pun" style="color: rgb(0, 0, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">2</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 创建节点</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> e </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"City"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setTextContent</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"崇文区"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 3. 使用父节点去在子节点之前添加新节点</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getParentNode</span><span class="pun" style="color: rgb(0, 0, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>insertBefore</strong></span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> node</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  save</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



9. 删除节点

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> removeNode</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 获取要删除的节点</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"street"</span><span class="pun" style="color: rgb(0, 0, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 找到他的父节点,删除子节点</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getParentNode</span><span class="pun" style="color: rgb(0, 0, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>removeChild</strong></span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">node</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  save</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



10. 设置节点属性

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> createAttribute</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 拿到[崇文区]这个节点</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"City"</span><span class="pun" style="color: rgb(0, 0, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">2</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 将node 转为element</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">node </span><span class="pun" style="color: rgb(0, 0, 0);">!=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">&&</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">instanceof</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> e </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> node</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);"><strong>setAttribute</strong></span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Code"</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">"103"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  save</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



11. 案例

学生信息存储体系

Student.java

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">package</span><span class="pln" style="color: rgb(0, 0, 0);"> com</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">itheima</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">beans</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;">
</code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Student</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> classid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> name</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> sex</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> score</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Student</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Student</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> classid</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> name</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> sex</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> score</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">classid </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> classid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">studentid </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">name </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> name</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sex </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> sex</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">score </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> score</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> getClassid</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> classid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> setClassid</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> classid</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">classid </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> classid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> getStudentid</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> setStudentid</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">studentid </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> getName</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> name</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> setName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> name</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">name </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> name</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> getSex</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> sex</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> setSex</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> sex</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sex </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> sex</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> getScore</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> score</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> setScore</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> score</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">this</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">score </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> score</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> toString</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">"Student [classid="</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> classid </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">", studentid="</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">", name="</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> name </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">", sex="</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> sex </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">", score="</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> score </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">"]"</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>


JAXP的工具类,包括文档获取方法和保存方法

JaxpUtils.java

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">package</span><span class="pln" style="color: rgb(0, 0, 0);"> com</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">itheima</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">utils</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22
4da86
px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> javax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">parsers</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilder</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> javax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">parsers</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilderFactory</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> javax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">transform</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Transformer</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> javax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">transform</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">TransformerFactory</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> javax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">transform</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">dom</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">DOMSource</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> javax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">transform</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">stream</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">StreamResult</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">w3c</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">dom</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="com" style="color: rgb(63, 127, 95);">// JAXP的工具类</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">JaxpUtils</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 获取document对象</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> getDocument</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilderFactory</span><span class="pln" style="color: rgb(0, 0, 0);"> factory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilder</span><span class="pln" style="color: rgb(0, 0, 0);"> builder </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">try</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      factory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DocumentBuilderFactory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newInstance</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      builder </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> factory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newDocumentBuilder</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      document </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> builder</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">parse</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"src/com/itheima/db/Students.xml"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">catch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throw</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">RuntimeException</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">" Can not get document !"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 保存文件</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> saveXML</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">TransformerFactory</span><span class="pln" style="color: rgb(0, 0, 0);"> factory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Transformer</span><span class="pln" style="color: rgb(0, 0, 0);"> tf </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">null</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">try</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      factory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">TransformerFactory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newInstance</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      tf </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> factory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newTransformer</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      tf</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">transform</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DOMSource</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">),</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">StreamResult</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"src/com/itheima/db/Students.xml"</span><span class="pun" style="color: rgb(0, 0, 0);">));</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">catch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throw</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">RuntimeException</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Save XML failed "</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>


对学生信息XML文档的操作

StudentDao.java

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">package</span><span class="pln" style="color: rgb(0, 0, 0);"> com</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">itheima</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">dao</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">impl</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">w3c</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">dom</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">w3c</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">dom</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">w3c</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">dom</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">w3c</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">dom</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">NodeList</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> com</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">itheima</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">beans</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Student</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> com</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">itheima</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">utils</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">JaxpUtils</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">StudentDAO</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 添加用户</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span><span class="pln" style="color: rgb(0, 0, 0);"> save</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Student</span><span class="pln" style="color: rgb(0, 0, 0);"> s</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 拿到document对象</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">JaxpUtils</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getDocument</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 创建Student节点</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> studentE </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Student"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 添加student节点的属性</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    studentE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAttribute</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"classid"</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> s</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getClassid</span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    studentE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAttribute</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"studentid"</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> s</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getStudentid</span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 3. 创建name节点</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> nameE </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"name"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 4. 给name节点添加文本内容</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    nameE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setTextContent</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">s</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getName</span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 5. 创建sex节点</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> sexE </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"sex"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 6. 给sex节点添加文本内容</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    sexE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setTextContent</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">s</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getSex</span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 7. 创建score节点</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> scoreE </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">createElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"score"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 8. 给score添加文本内容</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    scoreE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setTextContent</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">s</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getScore</span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 9. 逐级添加到节点上</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    studentE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">appendChild</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">nameE</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    studentE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">appendChild</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">sexE</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    studentE</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">appendChild</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">scoreE</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 10. 拿到Students(根节点)节点</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Students"</span><span class="pun" style="color: rgb(0, 0, 0);">).</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 11. 把 studentE添加到Students的节点上</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">appendChild</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">studentE</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 12. 将添加的内容保存到xml文件中</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">JaxpUtils</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">saveXML</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">true</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 删除用户</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span><span class="pln" style="color: rgb(0, 0, 0);"> remove</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span><span class="pln" style="color: rgb(0, 0, 0);"> result </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">false</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 拿到所有的学生信息</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">JaxpUtils</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getDocument</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">NodeList</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Student"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 遍历nlist列表</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(0, 0, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getLength</span><span class="pun" style="color: rgb(0, 0, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> i</span><span class="pun" style="color: rgb(0, 0, 0);">++)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="com" style="color: rgb(63, 127, 95);">// 拿到每一个节点</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">i</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="com" style="color: rgb(63, 127, 95);">// 判断n是不是属于元素节点,并且属于element类型</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getNodeType</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ELEMENT_NODE </span><span class="pun" style="color: rgb(0, 0, 0);">&&</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">instanceof</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(63, 127, 95);">// 强转成element</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> e </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(63, 127, 95);">// 使用element的getAttribute方法拿到studentid</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> sid </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getAttribute</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"studentid"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">sid</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">equals</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">studentid</span><span class="pun" style="color: rgb(0, 0, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          </span><span class="com" style="color: rgb(63, 127, 95);">// 删除当前节点,使用父节点操作</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getParentNode</span><span class="pun" style="color: rgb(0, 0, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">removeChild</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">e</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          </span><span class="typ" style="color: rgb(0, 0, 0);">JaxpUtils</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">saveXML</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">document</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          result </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">true</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">break</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> result</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">// 查询成绩</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> getScoreByStudentid</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> studentid</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> score </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">""</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 1. 拿到所有的学生信息</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">Document</span><span class="pln" style="color: rgb(0, 0, 0);"> document </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">JaxpUtils</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getDocument</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">NodeList</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> document</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getElementsByTagName</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"Student"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 2. 遍历nlist列表</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> i </span><span class="pun" style="color: rgb(0, 0, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getLength</span><span class="pun" style="color: rgb(0, 0, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> i</span><span class="pun" style="color: rgb(0, 0, 0);">++)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="com" style="color: rgb(63, 127, 95);">// 拿到每一个节点</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> nlist</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">i</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="com" style="color: rgb(63, 127, 95);">// 判断n是不是属于元素节点,并且属于element类型</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">n</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getNodeType</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ELEMENT_NODE </span><span class="pun" style="color: rgb(0, 0, 0);">&&</span><span class="pln" style="color: rgb(0, 0, 0);"> n </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">instanceof</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pln" style="color: rgb(0, 0, 0);"> e </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Element</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> n</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(63, 127, 95);">// 拿到学生ID</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> sid </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getAttribute</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"studentid"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(63, 127, 95);">// 判断学生id跟传进来的参数是否一致</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">sid</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">equals</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">studentid</span><span class="pun" style="color: rgb(0, 0, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          </span><span class="typ" style="color: rgb(0, 0, 0);">NodeList</span><span class="pln" style="color: rgb(0, 0, 0);"> nl </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getChildNodes</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">for</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> j </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> j </span><span class="pun" style="color: rgb(0, 0, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);"> nl</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getLength</span><span class="pun" style="color: rgb(0, 0, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> j</span><span class="pun" style="color: rgb(0, 0, 0);">++)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">            </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pln" style="color: rgb(0, 0, 0);"> node </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> nl</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">j</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">            </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getNodeType</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">ELEMENT_NODE </span><span class="pun" style="color: rgb(0, 0, 0);">&&</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(42, 0, 255);">"score"</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">equals</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getNodeName</span><span class="pun" style="color: rgb(0, 0, 0);">()))</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">              score </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> node</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getTextContent</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">              </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">break</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">            </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">          </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">return</span><span class="pln" style="color: rgb(0, 0, 0);"> score</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>


使用SAX方式解析XML文档


1. 常用API

SAX解析器工厂类:SAXParserFactory
SAX解析器:SAXParser
XML读取类:XMLReader
事件处理接口:ContentHandler
默认事件处理适配器类:DefaultHandler


2. 获取SAX解析并设置事件处理器

注意:必须先设置时间处理器,后设置XML文件源

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">static</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> main</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pun" style="color: rgb(0, 0, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> args</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">try</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 获取SAX解析器的工厂类</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">SAXParserFactory</span><span class="pln" style="color: rgb(0, 0, 0);"> factory </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(0, 0, 0);">SAXParserFactory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newInstance</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 获取SAX解析器</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">SAXParser</span><span class="pln" style="color: rgb(0, 0, 0);"> sax </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> factory</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">newSAXParser</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 获取XMLReader的读取对象</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">XMLReader</span><span class="pln" style="color: rgb(0, 0, 0);"> reader </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getXMLReader</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 设置事件处理器</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    reader</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setContentHandler</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DemoHandler</span><span class="pun" style="color: rgb(0, 0, 0);">());</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// 设置xml文件源</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    reader</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">parse</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"src/LocalList.xml"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">catch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">ParserConfigurationException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">|</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">|</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">IOException</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// TODO Auto-generated catch block</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    e</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">printStackTrace</span><span class="pun" style="color: rgb(0, 0, 0);">();</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



3. 通过直接实现事件处理接口定义事件处理器

DemoHandler.java

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">package</span><span class="pln" style="color: rgb(0, 0, 0);"> xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">handler</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Attributes</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">ContentHandler</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Locator</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DemoHandler</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">implements</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">ContentHandler</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> startDocument</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"文档开始"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> endDocument</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"文档结束"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> startElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> uri</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> localName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Attributes</span><span class="pln" style="color: rgb(0, 0, 0);"> atts</span><span class="pun" style="color: rgb(0, 0, 0);">)</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"元素开始:"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> endElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> uri</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> localName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"元素结束:"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> characters</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">char</span><span class="pun" style="color: rgb(0, 0, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> ch</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> start</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> length</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"文本内容:"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">ch</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> start</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> length</span><span class="pun" style="color: rgb(0, 0, 0);">));</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(63, 127, 95);">//------------以下采用空实现------------</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> ignorableWhitespace</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">char</span><span class="pun" style="color: rgb(0, 0, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> ch</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> start</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> length</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// TODO Auto-generated method stub</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> processingInstruction</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> target</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> data</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// TODO Auto-generated method stub</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> skippedEntity</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> name</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// TODO Auto-generated method stub</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> setDocumentLocator</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">Locator</span><span class="pln" style="color: rgb(0, 0, 0);"> locator</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// TODO Auto-generated method stub</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> startPrefixMapping</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> prefix</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> uri</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// TODO Auto-generated method stub</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> endPrefixMapping</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> prefix</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(63, 127, 95);">// TODO Auto-generated method stub</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



4. 通过继承事件处理默认适配器定义事件处理器

DemoAdapterHandler.java

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">package</span><span class="pln" style="color: rgb(0, 0, 0);"> xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">handler</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Attributes</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">helpers</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">DefaultHandler</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DemoAdapterHandler</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">extends</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DefaultHandler</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> startDocument</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"文档开始"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> endDocument</span><span class="pun" style="color: rgb(0, 0, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"文档结束"</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> startElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> uri</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> localName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Attributes</span><span class="pln" style="color: rgb(0, 0, 0);"> atts</span><span class="pun" style="color: rgb(0, 0, 0);">)</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"元素开始:"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> endElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> uri</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> localName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"元素结束:"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">);</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> characters</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">char</span><span class="pun" style="color: rgb(0, 0, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> ch</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> start</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> length</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"文本内容:"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">ch</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> start</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> length</span><span class="pun" style="color: rgb(0, 0, 0);">));</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>



5. 案例

元素计数及简单逻辑

DemoAdapterHandler.java

<div class="linenums" style="color: rgb(30, 52, 123); margin-top: 0px; margin-bottom: 0px; padding-left: 0px;"><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">package</span><span class="pln" style="color: rgb(0, 0, 0);"> xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">handler</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">Attributes</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">import</span><span class="pln" style="color: rgb(0, 0, 0);"> org</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">xml</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">sax</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">helpers</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="typ" style="color: rgb(0, 0, 0);">DefaultHandler</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DemoAdapterHandler</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">extends</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">DefaultHandler</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> count </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(85, 85, 85);">0</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">boolean</span><span class="pln" style="color: rgb(0, 0, 0);"> isElement </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">false</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> startElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> uri</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> localName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">Attributes</span><span class="pln" style="color: rgb(0, 0, 0);"> atts</span><span class="pun" style="color: rgb(0, 0, 0);">)</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"City"</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">equals</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">qName</span><span class="pun" style="color: rgb(0, 0, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      count</span><span class="pun" style="color: rgb(0, 0, 0);">++;</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    isElement </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">true</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> endElement</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> uri</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> localName</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> qName</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    isElement </span><span class="pun" style="color: rgb(0, 0, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">false</span><span class="pun" style="color: rgb(0, 0, 0);">;</span></code></div><div class="L3" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L4" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"> </code></div><div class="L5" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(85, 85, 85);">@Override</span></code></div><div class="L6" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">void</span><span class="pln" style="color: rgb(0, 0, 0);"> characters</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">char</span><span class="pun" style="color: rgb(0, 0, 0);">[]</span><span class="pln" style="color: rgb(0, 0, 0);"> ch</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> start</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">int</span><span class="pln" style="color: rgb(0, 0, 0);"> length</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">SAXException</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L7" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">if</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">count </span><span class="pun" style="color: rgb(0, 0, 0);">==</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(85, 85, 85);">5</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">&&</span><span class="pln" style="color: rgb(0, 0, 0);"> isElement</span><span class="pun" style="color: rgb(0, 0, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">{</span></code></div><div class="L8" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">      </span><span class="typ" style="color: rgb(0, 0, 0);">System</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">out</span><span class="pun" style="color: rgb(0, 0, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">println</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="str" style="color: rgb(42, 0, 255);">"第5个区是:"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(0, 0, 0);">+</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(127, 0, 85); font-weight: bold;">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(0, 0, 0);">String</span><span class="pun" style="color: rgb(0, 0, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">ch</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> start</span><span class="pun" style="color: rgb(0, 0, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> length</span><span class="pun" style="color: rgb(0, 0, 0);">));</span></code></div><div class="L9" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L0" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div><div class="L1" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);">  </span></code></div><div class="L2" style="color: rgb(190, 190, 197); line-height: 18px; list-style-type: none; padding-left: 0px !important;"><code class="language-java" style="font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; word-wrap: break-word; line-height: 22px;"><span class="pun" style="color: rgb(0, 0, 0);">}</span></code></div></div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: