1.
XML框架结构
Java SE 6 平台提供的 XML 处理主要包括两个功能:XML 处理(JAXP,Java Architecture XML Processing)和 XML 绑定(JAXB,Java Architecture XML Binding)。
JAXP 包括 SAX 框架 —— 遍历元素,做出处理;DOM 框架 —— 构造 XML 文件的树形表示;StAX 框架 —— 拖拽方式的解析;XSLT 框架 —— 将 XML 数据转换成其他格式。JAXB 则是负责将 XML 文件和 Java 对象绑定,在新版 JDK 中,被大量的使用在 Web 服务技术中。
目前的Java 操作xml的类库主要有:http://www.open-open.com/31.htm
2. stax解析
Ierator Event Types
Table 3-2 lists the thirteen XMLEvent
types defined in the event iterator API.
Event Type
|
Description
|
---|---|
StartDocument |
Reports the beginning of a set of XML events, including encoding, XML version, and standalone properties.
|
StartElement |
Reports the start of an element, including any attributes and namespace declarations; also provides access to the prefix, namespace URI, and local name of the start tag.
|
EndElement |
Reports the end tag of an element. Namespaces that have gone out of scope can be recalled here if they have been explicitly set on their corresponding
StartElement . |
Characters |
Corresponds to XML
CData sections and CharacterData entities. Note that ignorable whitespace and significant whitespace are also reported as Character events. |
EntityReference |
Character entities can be reported as discrete events, which an application developer can then choose to resolve or pass through unresolved. By default, entities are resolved. Alternatively, if you do not want to report the entity as an event, replacement text can be substituted and reported as
Characters . |
ProcessingInstruction |
Reports the target and data for an underlying processing instruction.
|
Comment
|
Returns the text of a comment
|
EndDocument
|
Reports the end of a set of XML events.
|
DTD
|
Reports as
java.lang.String information about the DTD, if any, associated with the stream, and provides a method for returning custom objects found in the DTD. |
Attribute
|
Attributes are generally reported as part of a
StartElement event. However, there are times when it is desirable to return an attribute as a standalone Attribute event; for example, when a namespace is returned as the result of an XQuery or XPath expression. |
Namespace
|
As with attributes, namespaces are usually reported as part of a
StartElement , but there are times when it is desirable to report a namespace as a discrete Namespace event. |
Sample Event Mapping
As an example of how the event iterator API maps an XML stream, consider the following XML document:
<?xml version="1.0"?>
<BookCatalogue xmlns="http://www.publishing.org">
<Book>
<Title>Yogasana Vijnana: the Science of Yoga</Title>
<ISBN>81-40-34319-4</ISBN>
<Cost currency="INR">11.50</Cost>
</Book>
</BookCatalogue>
This document would be parsed into eighteen primary and secondary events, as shown below. Note that secondary events, shown in curly braces ({}
), are typically accessed from a primary event rather than directly.
Table 3-3 Sample Iterator API Event Mapping |
||
# |
Element/Attribute |
Event |
1 |
version="1.0" |
StartDocument |
2 |
isCData = false |
Characters |
3 |
qname = BookCatalogue:http://www.publishing.org |
StartElement |
4 |
qname = Book |
StartElement |
5 |
qname = Title |
StartElement |
6 |
isCData = false |
Characters |
7 |
qname = Title |
EndElement |
8 |
qname = ISBN |
StartElement |
9 |
isCData = false data = "81-40-34319-4\n\t" IsWhiteSpace = false |
Characters |
10 |
qname = ISBN |
EndElement |
11 |
qname = Cost |
StartElement |
12 |
isCData = false |
Characters |
13 |
qname = Cost |
EndElement |
14 |
isCData = false |
Characters |
15 |
qname = Book |
EndElement |
16 |
isCData = false |
Characters |
17 |
qname = BookCatalogue:http://www.publishing.org |
EndElement |
18 |
EndDocument |