DOC PREVIEW
MSU ECE 480 - Introduction of parsing XML data using JDOM

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction of parsing XML data using JDOMSungsoo Kim April 6, 2009AbstractThis document describes what XML is and how to receive information using XML feedback. Examples are shown using XML feedback sent by weather.com. The first section of this application note introduces what is XML. The second section describes how to retrieve XML data and how to parse the information using JDOM. This application note assumes the reader is familiar with Java.KeywordsJAVA, XML, Parser, JDOM, weather.comObjectives Introduce XML Teach how to retrieve the data from XML document using JDOM1 IntroductionXML is widely used for exchanging information on the World Wide Web. It stands for eXtensible Markup Language. Extensible means that the user can define its own structure according to its purpose. Markup Language is a system of how the document is to be described or logically structured. The two most famous Markup Languages are XML and HTML. While HTML is used for displaying data on the web browser, XML is used for carrying data on the web. Compatibility is the greatest strength of XML. Data written in XML format can be shared between any different kinds of computers, different kinds of operating systems or different kinds of applications. Applications can define their own data structure in XML format so that different applications can communicate data and interpret for its own purpose using different XML Parser. For example, Safari Web browser running on Macbook can interpret XML data written in Microsoft Windows running on Intel i7 processor. XML is based on international standards and supports Unicode that it supports languages other than English. Its compatibility also makes it easier to upgrade or expand to new applications or new browsers without losing old data.To begin, it is necessary to overview the XML structure and parsing in order to understand how it is used.2 Structure of XMLXML syntax is quite strict. Only “Well-Formed” or “Validated” XML data can be used by other XML parsers. In this section, a structure of “Well-Formed” XML is discussed. Figure 1 shows actual data written in XML format. Figure 1. XML data sent by weather.com2.1 DeclarationFigure 2. XML DeclarationXML document declaration includes the information of the XML version and how it is encoded. XML declaration is always placed in the top line. There are two versions of XML, XML1.0 and XML 1.1. To avoid confliction, the version information is declared in the XML declaration. Since XML supports Unicode, data can be written in many languages other than English. Encoding information must be included to inform the receiver of data which language this XML data written. Any instructions declared between <? and ?> is called as processing instruction. <?xml version=”1.0”?> is a processing instruction, however XML calls it as declaration.2.2 CommentsFigure3. Comments in XMLComments are used to inform the user of XML data or help the user understand the data structure. All XML comments begin with <!-- and close with -->. XML comments can be placed anywhere.2.3 Elements and AttributeFigure 4. Elements in XMLElements contain the actual data of XML document. Elements must have a start-tag and an end-tag which contain the element’s name. Element content sits between these two tags. Elements have a tree-based data structure. In the example in Figure 4, dnam, tm and other elements but loc have exact values and are indented, which mean these elements are children of the loc element. Figure 5. Illustrates Figure 4 as a tree-based data structureThere is only one root element per each XML document, which contains all of the data’s information. In figure 1, the root element is weather.In the start-tag of the loc element, there is one piece of information, id. The information included in the start-tag is called Attribute. Attribute makes XML documents easy to understand and clarify data. The value of attribute must be quoted, using either single quotes or double quotes.Figure 6. Syntax for using Element and AttributeXML also provides ways to represent empty element contents. By adding a slash after the element name in start-tag, “Empty element” can be represented. Empty element can have attribute name and values.2.4 Entity Entity is often used in XML to represent single characters which are not easy to typed on the keyboard, for example, “&”,”<”, and “>”. Entity reference consists of an ampersand, the entity’s name, and a semicolon. Some special characters’ entity values are predefined. For example, the ampersand is defined as “&amp;”. Undefined specialcharacters can be defined by the user in the document’s Document Type Definition (DTD).3. Parse XML data After receiving XML data from the World Wide Web, there must be some methods to handle XML data. A parser for XML is used to handle data in XML format. In computer science, a parser is one of the components in a complier, which checks for correct syntax and builds a data structure. There are several types of parsers, but SAX (Simple API(Application Programming Interface) for XML) and DOM (Document Object Model for XML) are the two most commonly used parsers. However, SAX uses event-driven API so that previously parsed data cannot be re-read. DOM has a complicated API and is slow compared to SAX. To compliment the disadvantages of these two parsers, JDOM was developed by Jason Hunter and Brett McLaughlin in March 2000. JDOM integrates DOM and SAX and it is designed for JAVA platform. The following covers parsing XML documents using JDOM.3.1 Creating XML DocumentTo create an XML document, the first thing to do is declare the Document object.Figure 7. Example of the declaration of Document objectIn the first line, the user defines which builder, either SAXBuilder or DOMBuilder, is used. In this application note, SAXBuilder is used. Declare SAXBuilder and use Build function in SAXBuilder to create document object like Figure 7. Build function is used to retrieve the XML data from either a file in local drive or the server on the web. Appendix A shows the usage of Build function. Now, all XML data is in the Document object. 3.2 Parse XML dataAfter creating an XML document, the user can parse any data from the document object. JDOM provides several functions to parse the XML documents and return XML document’s Attributes value, Entities, and Elements. The getRootElement function, the getChild function and the getText function are the most


View Full Document

MSU ECE 480 - Introduction of parsing XML data using JDOM

Documents in this Course
ganttv1

ganttv1

6 pages

sd97

sd97

17 pages

ap_EO

ap_EO

14 pages

Load more
Download Introduction of parsing XML data using JDOM
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction of parsing XML data using JDOM and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction of parsing XML data using JDOM 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?