2019年11月10日 星期日

Java StAX學習與建構1/2


StAX使用說明

StAX規範定義了API的許多例子:

  • 資料綁定
  • 解組XML文檔
  • 編組XML文檔
  • 並行文件處理
  • 無線通信
  • 簡單對象訪問協議(SOAP)消息處理
  • 解析簡單的可預測結構
  • 解析具有前向引用的圖形表示
  • 解析Web服務描述語言(WSDL)
  • 虛擬數據源
  • 查看存儲在數據庫中的XML數據
  • 查看通過XML數據綁定創建的Java對像中的數據
  • 將DOM樹作為事件流進行導航
  • 解析特定的XML詞彙表
  • 流水線XML處理

StAX與其他JAXP API進行比較:

StAX不如TrAX或JDOM強大或靈活,但也不需要那麼多的內存或處理器負載就可以使用,並且StAX在許多情況下都可以勝過基於DOM的API。在StAX和SAX之間進行最接近的比較,正是在這裡StAX提供了許多情況下都有益的功能。其中一些包括:
  • 啟用StAX的客戶端通常比SAX客戶端更易於編碼。儘管可以說SAX解析器在編寫上稍微容易一些,但StAX解析器代碼可以更小,並且客戶端與解析器進行交互所必需的代碼也更簡單。
  • StAX是雙向API,這意味著它可以讀取和寫入XML文檔。SAX是只讀的,因此如果要編寫XML文檔,則需要另一個API。
  • SAX是推式API,而StAX是拉式。上面概述的推式和拉式API之間的權衡在這裡適用。
FeatureStAXSAXDOMTrAX
API TypePull, streamingPush, streamingIn memory treeXSLT Rule
Ease of UseHighMediumHighMedium
XPath CapabilityNoNoYesYes
CPU and Memory EfficiencyGoodGoodVariesVaries
Forward OnlyYesYesNoNo
Read XMLYesYesYesYes
Write XMLYesNoYesYes
Create, Read, Update, DeleteNoNoYesNo

環境設置

為了使用StAX的解析器,應該準備好stax.jar在應用程序的類路徑中。官方網站範例說明,下載 stax-1.2.0.jar.

以下是StAX API的功能

  • 讀取XML文件從上到下,認識構成一個結構完整的XML文檔的標記
  • 令牌是以相同的順序進行處理,它們出現在文檔中
  • 報告應用程序,因為解析器遇到標記的特性
  • 應用程序提供了一個“事件”讀取器充當了事件,以獲得所需信息的迭代器和迭代。可另一個讀取器是“光標”充當一個指向XML節點。
  • 由於事件被識彆,XML元素可以從事件對象進行檢索,並且可以進一步處理。

XMLEventReader類

因為在解析XML文檔時該類提供可用於迭代事件事件迭代器
  • StartElement asStartElement() - 用於檢索值和元素的屬性。
  • EndElement asEndElement() - 調用元件的端部。
  • Characters asCharacters() - 可用於獲得字符,例如一個CDATA,空白等。

XMLEventWriter類

此接口指定創建事件的方法。
  • add(Event event) - 添加包含元素XML事件。

XMLStreamReader Class

因為在解析XML文檔時該類提供可用於迭代事件事件迭代器
  • int next() - 用於檢索下一個事件。
  • boolean hasNext() - 用於檢查其他事件的存在與否
  • String getText() - 用於獲取一個元素的文本
  • String getLocalName() - 用於獲取一個元素的名稱

XMLStreamWriter類

此接口指定創建事件的方法
  • writeStartElement(String localName) - 加入定名稱開始元素。
  • writeEndElement(String localName) - 添加指定名稱的結束元素。
  • writeAttribute(String localName, String value) - 編寫屬性到元素






StAX Factory Classes


XMLInputFactoryXMLOutputFactoryXMLEventFactory,使您可以定義和配置XML流讀取器,流寫入器和事件類的實現實例。
  1. Use the javax.xml.stream.XMLInputFactory system property.
  1. Use the lib/xml.stream.properties file in the Java SE platform's Java Runtime Environment (JRE) directory.
  1. Use the Services API, if available, to determine the classname by looking in the META-INF/services/javax.xml.stream.XMLInputFactory files in JAR files available to the JRE.
  1. Use the platform default XMLInputFactory instance.


Deriving from JAXP, the XMLInputFactory.newInstance method determines the specific XMLInputFactory implementation class to load by using the following lookup procedure:

After getting a reference to an appropriate XMLInputFactory, an application can use the factory to configure and create stream instances. The following table lists the properties supported by XMLInputFactory. See the StAX specification for a more detailed listing.

javax.xml.stream.XMLInputFactory Properties
PropertyDescription
isValidatingTurns on implementation-specific validation.
isCoalescing(Required) Requires the processor to coalesce adjacent character data.
isNamespaceAwareTurns off namespace support. All implementations must support namespaces. Support for non-namespace-aware documents is optional.
isReplacingEntityReferences(Required) Requires the processor to replace internal entity references with their replacement value and report them as characters or the set of events that describe the entity.
isSupportingExternalEntities(Required) Requires the processor to resolve external parsed entities.
reporter(Required) Sets and gets the implementation of the XMLReporter interface.
resolver(Required) Sets and gets the implementation of the XMLResolver interface.
allocator(Required) Sets and gets the implementation of the XMLEventAllocator interface.

沒有留言:

ACCESS資料成功轉換SQlite方法

最近忙於系統開發研究,今日來談一下有朋友很有才華自行利用ACCESS開發了CRM(客戶關係管理)系統,但僅限於個人使用考量目前大量使用者成長與需求,洽詢我是否能技術支援同時給了我ACCESS檔案參考,但因電腦OFFICE沒有ACCESS可開啟.accdb檔案,相信有許多微軟OFF...