• Java xml 解析


    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.

    Table 3-2 XMLEvent Types 
    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
    data = " "
    IsWhiteSpace = true

    Characters

    3

    qname = BookCatalogue:http://www.publishing.org
    attributes = null
    namespaces = {BookCatalogue" -> http://www.publishing.org"}

    StartElement

    4

    qname = Book
    attributes = null
    namespaces = null

    StartElement

    5

    qname = Title
    attributes = null
    namespaces = null

    StartElement

    6

    isCData = false
    data = "Yogasana Vijnana: the Science of Yoga "
    IsWhiteSpace = false

    Characters

    7

    qname = Title
    namespaces = null

    EndElement

    8

    qname = ISBN
    attributes = null
    namespaces = null

    StartElement

    9

    isCData = false

    data = "81-40-34319-4 "

    IsWhiteSpace = false

    Characters

    10

    qname = ISBN
    namespaces = null

    EndElement

    11

    qname = Cost
    attributes = {"currency" -> INR}
    namespaces = null

    StartElement

    12

    isCData = false
    data = "11.50 "
    IsWhiteSpace = false

    Characters

    13

    qname = Cost
    namespaces = null

    EndElement

    14

    isCData = false
    data = " "
    IsWhiteSpace = true

    Characters

    15

    qname = Book
    namespaces = null

    EndElement

    16

    isCData = false
    data = " "
    IsWhiteSpace = true

    Characters

    17

    qname = BookCatalogue:http://www.publishing.org
    namespaces = {BookCatalogue" -> http://www.publishing.org"}

    EndElement

    18

     

    EndDocument

  • 相关阅读:
    java运算符优先级
    快排
    dpkg
    BZOJ 4487 染色问题
    BZOJ 3530 数数
    XSY 2754 求和
    BZOJ 4559 成绩比较
    广义容斥-二项式反演-容斥系数
    线性基学习笔记及其相关证明
    BZOJ 2754 喵星球上的点名
  • 原文地址:https://www.cnblogs.com/ranger2016/p/3872796.html
Copyright © 2020-2023  润新知