Pobieranie prezentacji. Proszę czekać

Pobieranie prezentacji. Proszę czekać

SAX i DOM wykorzystanie XML-a we własnych aplikacjach

Podobne prezentacje


Prezentacja na temat: "SAX i DOM wykorzystanie XML-a we własnych aplikacjach"— Zapis prezentacji:

1 SAX i DOM wykorzystanie XML-a we własnych aplikacjach
Simple API for XML Parsing Document Object Model

2 SAX - wstęp Definicja: standardowy interfejs do parsowania plików XML oparty na modelu zdarzeniowym stworzony w celu ujednolicenia dostępu do różnych parserów XML Cechy: model zdarzeniowy obiektowość prostota sekwencyjność szybkość status - darmowy prototypowany w Javie

3 SAX - historia 1998r. - SAX 2000r. - SAX2

4 Ilustracja działania SAX
setContentHandler Parser - XMLReader parse Przykład wywołania: <tekst> Aplikacja <tytul styl="ital"> Spojrzenia na dokument XML startDocument startElement </tytul> ignorableWhitespace </autor> startElement </tekst> characters endElement ignorableWhitespace throw SAXException

5 SAX - możliwości SAX umożliwia: łatwą zmianę samego parsera
porównywanie parserów stosowanie różnych parserów

6 Różnice między SAX i SAX2
Obsługa przestrzeni nazw Cechy (features) - wartości boolowskie Właściwości (properties) - dowolne obiekty Zmiany nazw wielu obiektów Changes from SAX 1.0 to SAX 2.0beta The following interfaces and classes have been deprecated, and will be removed from a future version of SAX; they should be used only for interaction with SAX1 drivers or applications: org.xml.sax.Parser org.xml.sax.DocumentHandler org.xml.sax.AttributeList org.xml.sax.HandlerBase org.xml.sax.helpers.ParserFactory org.xml.sax.helpers.AttributeListImpl The following interfaces and classes have been added to SAX2: org.xml.sax.XMLReader (replaces Parser) org.xml.sax.XMLFilter org.xml.sax.ContentHandler (replaces DocumentHandler) org.xml.sax.Attributes (replaces AttributeList) org.xml.sax.SAXNotSupportedException org.xml.sax.SAXNotRecognizedException org.xml.sax.helpers.AttributesImpl (replaces AttributeListImpl) org.xml.sax.helpers.NamespaceSupport org.xml.sax.helpers.XMLFilterImpl org.xml.sax.helpers.ParserAdapter org.xml.sax.helpers.XMLReaderAdapter org.xml.sax.helpers.DefaultHandler (replaces HandlerBase) org.xml.sax.ext.LexicalHandler (since removed) org.xml.sax.ext.DeclHandler (since removed) SAX2 contains complete Namespace support, which is available by default from any XMLReader. An XML reader can also optionally supply raw XML 1.0 names. See the Namespaces page for more details. An XML reader is fully configurable: it is possible to attempt to query or change the current value of any feature or property. Features and properties are identified by fully-qualified URIs, and parties are free to invent their own names for new extensions. See the Features and Properties page for more details. The ContentHandler and Attributes interfaces are similar to the deprecated DocumentHandler and AttributeList interfaces, but they add support for Namespace-related information. ContentHandler also adds a callback for skipped entities, and Attributes adds the ability to look up an attribute's index by name. The ParserAdapter class makes a SAX1 Parser behave as a SAX2 XMLReader. The XMLReaderAdapter class makes a SAX2 XML reader behave as a SAX1 parser. These two classes should ease the transition from SAX1 to SAX2 by allowing SAX1 drivers and clients to co-exist with SAX2 drivers and clients in the same application.

7 SAX2 - pakiet org.xml.sax Interfejsy implementowane przez parser: XMLReader parse (2 metody) setDocumentHandler ... Attributes getLength getName (2 metody) getValue (2 metody) Opcjonalny: Locator Interfejsy implementowane przez użytkownika parsera: ContentHandler characters, ignorableWhitespace startDocument, endDocument startElement, endElement processingInstruction setDocumentLocator ErrorHandler, DTDHandler, EntityResolver

8 SAX2 - pakiet org.xml.sax Standardowa klasa:
org.xml.sax.InputSource - może pobierać dane z InputStream, Reader, String Wyjątek: SAXException - ten wyjątek jest podnoszony w przypadku wystąpienia błędu Klasy pomocnicze (pakiet org.xml.sax.helpers): DefaultHandler - aplikację tworzymy tworząc podklasę tej klasy XMLReaderFactory AttributesImpl LocatorImpl

9 SAX kroki implementacji
1. Tworzymy podklasę klasy org.xml.sax.helpers.DefaultHandler 2. Pobieramy obiekt org.xml.sax.XMLReader z fabryki 3. Rejestrujemy stworzoną podklasę w parserze (XMLReader) metodami set<....>Handler 4. Wywołujemy metodę parse

10 Filtry SAX Co to są Filtry SAX
Implementują interfejs: org.xml.sax.XMLFilter Rozszerzają klasę: org.xml.sax.helpers.XMLFilterImpl Specyficzne implementacje interfejsów: ContentHandler, DTDHandler, EntityResolver, ErrorHandler Można je łączyć w łańcuchy: XMLReader reader; ... XMLFilterImpl f1 = new XMLFilterImpl(reader); XMLFilterImpl f2 = new XMLFilterImpl(f1); f2.parse(...);

11 Przykładowe parsery SAX
Java: Xerces-J (SAX2), SUN XML Parser, Oracle XML Parser, C/C++: Microsoft XML Parser, Xerces-C (SAX2), expat, Python: Pakiet 4XML Perl: XML::Parser (obudowa expat’a w Perlu)

12 DOM Definicja: obiektowy model dostępu, stukturalizacji i nadawania stylu dokumentom DOM - oficjalna rekomendacja W3C DOM2 - kandydat na oficjalną rekomendację dla XML-a najistotniejsza jest część DOM Core, czyli właściwa definicja interfejsu obiektowego IDL - Interface Definition Language - język służący do zapisu interfejsów obiektowych niezależny od języka programowania

13 DOM Core Bazowa cześć specyfikacji DOM Umożliwia: budowanie dokumentów
nawigację po strukturze dokumentów dodawanie elementów modyfikacje elementów usuwanie elementów i ich zawartości Wady: pamięciożerność niska efektywność

14 DOM Core DOM Core ma swoje ustalone tłumaczenie na Javę,
tłumaczeń na C++ jest wiele, obiekty DOM są dostępne jako obiekty COM – np. parser MSXML3

15 DOM - prezentacja graficzna
Prezentacja graficzna obiektu (drzewa) DOM i odpowiadającego mu dokumentu XML <TABLE BORDER="1"> <TR> <TD>Shady Grove</TD> <TD>Aeolian</TD> </TR> <TD>Over the River, Charlie</TD> <TD>Dorian</TD> </TABLE> Document TABLE: Element TR TR BORDER:Attribute TD TD TD TD Shady Grove : Text Aeolian Over the River, Charlie Dorian

16 DOM – diagram najważniejszych interfejsów
Node Document Element Comment Processing Instruction Attr Text CDATA Section

17 Interfejs Node dostęp do zawartości manipulacja zawartością
getAttributes() getChildNodes() getFirstChild() getLastChild() getNextSibling () getPreviousSibling () getNodeName() getNodeValue() getNodeType() getOwnerDocument() getParentNode() hasChildNodes() manipulacja zawartością appendChild(Node) insertBefore(Node, Node) removeChild(Node) replaceChild(Node, Node) setNodeValue(String) setNodeName(String) klonowanie cloneNode(boolean) typy obiektów ATTRIBUTE_NODE, ...

18 Klasy pomocnicze DOM NamedNodeMap
tablica haszująca obiektów Node (atrybuty!) NodeList wektor obietków Node (dzieci danego węzła!) DOMException wyjątek podnoszony, w przypadku błędnej modyfikacji węzła

19 DOM a DOM2 - różnice Najważniejsze różnice między DOM, a DOM2
możliwość łączenia zawartości dwóch dokumentów obsługa przestrzeni nazw

20 SAX  DOM Przetwarzanie wsadowe.
Całe drzewo dokumentu ładowane do pamięci. Oszczędny czasowo i pamięciowo. Kosztowny czasowo i pamięciowo. Dobry do wyławiania z dokumentu wybranych elementów. Pozwala wędrować po drzewie dokumentu. Dokument tylko do odczytu Pozwala tworzyć i modyfikować dokumenty.


Pobierz ppt "SAX i DOM wykorzystanie XML-a we własnych aplikacjach"

Podobne prezentacje


Reklamy Google