SAX不能正确处理特殊字符的转义实体?

jiangshachina jiangshachina 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
刚刚学习使用SAX解析XML,遇到了两个问题。

全部程序如下:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SimpeXmlHandler extends DefaultHandler {

private String str = null;

private String element = null;

public void startElement(String namespaceURI, String localName,
String fullName, Attributes attributes) throws SAXException {
element = fullName;
for (int i = 0; i < attributes.getLength(); i++) {
String qName = attributes.getQName(i);
if (qName.equals("id")) {
System.out.println("id=" + attributes.getValue(qName).trim());
break;
}
}
}

public void endElement(String uri, String localName, String qName)
throws SAXException {
if (str != null) {
if (element.equalsIgnoreCase("title")) {
System.out.println("title=" + str);
} else if (element.equalsIgnoreCase("href")) {
System.out.println("href=" + str);
} else if (element.equalsIgnoreCase("content")) {
System.out.println("content=" + str);
}
}
}

public void characters(char[] chars, int start, int length)
throws SAXException {
str = new String(chars, start, length).trim();
}
}

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class SimpleXmlTest {

public static void main(String[] args) throws Exception {
SimpeXmlHandler handler = new SimpeXmlHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
SAXParser parser = factory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(handler);
InputSource source = new InputSource("config/sample.xml");
xmlReader.parse(source);
}
}

执行SimpleXmlTest解析如下的XML文件,
<?xml version="1.0" encoding="UTF-8"?>
<root>
<articles>
<article id="00001">
<title>titleValue</title>
<href>hrefValue</href>
<publishtime>timeValue</publishtime>
<content>contentValue</content>
<tag>0</tag>
</article>
</articles>
</root>
结果如下,
id=00001
title=titleValue
href=hrefValue
content=contentValue

将XML文件的内容换成如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<articles>
<article id="00001">
<title>titleValue</title>
<href>hrefValue</href>
<publishtime>timeValue</publishtime>
<content>start>end</content>
<tag>0</tag>
</article>
</articles>
</root>
执行程序后会得到如下结果:
id=00001
title=titleValue
href=hrefValue
content=end

再将XML文件的内容换成如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<articles>
<article id="00001">
<title>titleValue</title>
<href>hrefValue</href>
<publishtime>timeValue</publishtime>
<content>start>end</content>
<tag>0</tag>
</article>
</articles>
</root>
再次执行程序后会得到如下结果:
id=00001
title=titleValue
href=hrefValue
content=start>end

似乎SAX会自动地把">"转换成">",这样就造成了错误。
一般地,在XML文件中直接使用">","<","&",...等特殊字符会造成错误,所以会使用">","<","&",...等转义实体。
但在我的程序中,似乎恰恰与此相反。
如何解释上述情况呢?

另,将XML文件的内容换成如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<articles>
<article id="00001">
<title>titleValue</title>
<href>hrefValue</href>
<publishtime>timeValue</publishtime>
<content>contentValue</content>
<!-- <tag>0</tag> -->
</article>
</articles>
</root>
执行测试程序后会得到如下结果:
id=00001
title=titleValue
href=hrefValue
content=contentValue
content=
content=
content=
对于最后三行的"content=",我不能理解。

希望大家能为我解惑,谢谢!
...全文

相关推荐

Python对HTML转义字符进行反转义的实现方法

什么是转义字符 在 HTML 中 &lt;、>、& 等字符有特殊含义(&lt;,> 用于标签中,& 用于转义),他们不能在 HTML 代码中直接使用,如果要在网页中显示这些符号,就需要使用 HTML 的转义字符串(Escape Sequence),例如 &lt; 的转义字符是 &lt;,浏览器渲染 HTML 页面时,会自动把转移字符串换成真实字符。 转义字符(Escape Sequence)由三部分组成:第一部分是一个 & 符号,第二部分是实体(Entity)名字,第三部分是一个分号。 比如,要显示小于号(&lt;),就可以写&lt; 。 Python 转义字符串反转义 用 Python 来处理转义字符串有多种方式,而且

weixin_38709466

解析XML特殊字符方法

解析XML特殊字符方法有二种 XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符XML特殊字符

oubo824219

Android常见XML转义字符(总结)

在安卓开发中我们经常会遇到一些特殊字符,这些特殊字符在程序中无法直接写,那么如何显示这些字符呢?这里就需要使用转义字符,所有编程语言,拥有转义字符的原因基本上是两点: 一、使用转义字符来表示字符集中定义的字符,比如ASCll里面的控制字符及回车换行等字符,这些字符都没有现成的文字代号。所以只能用转义字符来表示 。 二、某一些特定的字符在编辑语言中被定义为特殊用途的字符。这些字符由于被定义为特殊用途,它们失去了原有的意义。 以下为部分XML标志符的数字和字符串转义符:       “     双引号    ( 或 )       ‘     单引号    (' 或 ')       & 

weixin_38644688

详解用Python处理HTML转义字符的5种方式

本文介绍了详解用Python处理HTML转义字符的5种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

weixin_38513665

sax读取xml文件并反射到对应的实体中

sax读取xml文件并反射到对应的实体中

灰祥的奇迹
评论 3
收藏 已收藏
分享
返回顶部
jiangshachina
Java EE 67,535 成员 225,843 内容