1 / 17

XML Parsers

XML Parsers. A n XML parser is a software library or package that provides interfaces for client applications to work with an XML document. The XML Parser is designed to read the XML and create a way for programs to use XML. Types of XML Parsers.

alh
Télécharger la présentation

XML Parsers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Parsers An XML parser is a software library or package that provides interfaces for client applications to work with an XML document. The XML Parser is designed to read the XML and create a way for programs to use XML.

  2. Types of XML Parsers • These are the two main types of XML Parsers: • DOM • SAX

  3. DOM (Document Object Model) • A DOM document is an object which contains all the information of an XML document. It is composed like a tree structure. The DOM Parser implements a DOM API. This API is very simple to use.

  4. Features of DOM Parser • A DOM Parser creates an internal structure in memory which is a DOM document object and the client applications get information of the original XML document by invoking methods on this document object. • DOM Parser has a tree based structu

  5. Advantages • 1) It supports both read and write operations and the API is very simple to use. • 2) It is preferred when random access to widely separated parts of a document is required.

  6. Disadvantages • 1) It is memory inefficient. (consumes more memory because the whole XML document needs to loaded into memory). • 2) It is comparatively slower than other parsers.

  7. DOM interfaces • The DOM defines several Java interfaces. Here are the most common interfaces − • Node − The base datatype of the DOM. • Element − The vast majority of the objects you'll deal with are Elements. • Attr − Represents an attribute of an element. • Text − The actual content of an Element or Attr. • Document − Represents the entire XML document. A Document object is often referred to as a DOM tree.

  8. Common DOM methods • When you are working with DOM, there are several methods you'll use often − • Document.getDocumentElement() − Returns the root element of the document. • Node.getFirstChild() − Returns the first child of a given Node. • Node.getLastChild() − Returns the last child of a given Node. • Node.getNextSibling() − These methods return the next sibling of a given Node. • Node.getPreviousSibling() − These methods return the previous sibling of a given Node. • Node.getAttribute(attrName) − For a given Node, it returns the attribute with the requested name.

  9. Steps to Using JDOM Following are the steps used while parsing a document using JDOM Parser. • Import XML-related packages. • Create a SAXBuilder. • Create a Document from a file or stream • Extract the root element • Examine attributes • Examine sub-elements

  10. SAX (Simple API for XML) • A SAX Parser implements SAX API. This API is an event based API and less intuitive.

  11. Features of SAX Parser • It does not create any internal structure. • Clients does not know what methods to call, they just overrides the methods of the API and place his own code inside method. • It is an event based parser, it works like an event handler in Java.

  12. Advantages • 1) It is simple and memory efficient. • 2) It is very fast and works for huge documents.

  13. Disadvantages • 1) It is event-based so its API is less intuitive. • 2) Clients never know the full information because the data is broken into pieces.

  14. ContentHandlerInterface • This interface specifies the callback methods that the SAX parser uses to notify an application program of the components of the XML document that it has seen. • void startDocument() − Called at the beginning of a document. • void endDocument() − Called at the end of a document. • void startElement(String uri, String localName, String qName, Attributes atts) − Called at the beginning of an element. • void endElement(String uri, String localName,StringqName) − Called at the end of an element.

  15. ContentHandler Interface • void characters(char[] ch, int start, int length) − Called when character data is encountered. • void ignorableWhitespace( char[] ch, int start, int length) − Called when a DTD is present and ignorable whitespace is encountered. • void processingInstruction(String target, String data) − Called when a processing instruction is recognized. • void setDocumentLocator(Locator locator)) − Provides a Locator that can be used to identify positions in the document.

  16. ContentHandler Interface • void skippedEntity(String name) − Called when an unresolved entity is encountered. • void startPrefixMapping(String prefix, String uri) − Called when a new namespace mapping is defined. • void endPrefixMapping(String prefix) − Called when a namespace definition ends its scope.

  17. Attributes Interface • This interface specifies methods for processing the attributes connected to an element. • intgetLength() − Returns number of attributes. • String getQName(int index) • String getValue(int index) • String getValue(String qname)

More Related