XML Tutorial - XML Master Professional Application Developer Edition
Volume 2 : DOM/SAX
Tatsuya Kimura
Scope of DOM Questions Appearing on the Exam
All DOM-related questions on the XML Master exam are based on "DOM Level 2," and mainly deal with "DOM Level 2 Core." Section 1 consists of questions that test basic knowledge in this area.
Scope of SAX Questions Appearing on the Exam
All Questions related to SAX on the XML Master Exam are based on "SAX 2.0." SAX 2.0 includes "SAX 2.0 Extensions" (extended specification); however, exam questions in this area focus mainly on areas not concerned with this extended specification (having said that, you still must have an understanding of the basics of SAX 2.0 Extensions).
Section 1 Major Points for Study
Section 1 presents questions related to the purpose, characteristics and API specifications of DOM and SAX-all questions here will be word questions. Accordingly, you need to have a good understanding of DOM/SAX basic specifications to be successful in this section.
Examples of Questions Appearing in Section 1 - DOM
Most interfaces defined under DOM Level 2 Core are basic interfaces that are used frequently in actual programming. Nevertheless, there are also infrequently used interfaces (Notation, Entity, EntityReference, etc.). While you have to have a general understanding of these, a better understanding of this area as required for practical work applications is probably sufficient. *1
*1 | This applies not only to Section 1, but also to questions related to DOM in Sections 2 and 6 as well. |
JAXP (Java API for XML Processing) is frequently used when performing DOM programming in Java; however, you will not be asked about the specific JAXP API in the XML Master Exam.
To answer the questions in Section 1, it's important to understand what you "can" and "can't" do with the DOM Level 2 API. Let's provide an example here. Which of the following two sentences is correct?
- The method for changing the element node name (nodeName) is defined under DOM Level 2
- The method for obtaining the attribute node (Attr) by using the text string of an attribute name as an argument is defined under DOM Level 2
DOM Level 2 has no method for changing the element node name. Accordingly, statement "1." is incorrect. If you want to change the element node name under DOM Level 2, you must first create an element node with a different name, after which you must move all information (including the attributes and child nodes of the original element node) to the new element node. *2
*2 | Under DOM Level 3, a method (renameNode) achieving the role as described in statement "1." has been added to the interface Document. |
Statement "2." is correct. The method applicable to the method explained in "2." is the interface Element method getAttributeNode (use the getAttributeNodeNS method when obtaining the attribute node that belongs to XML namespaces).
Questions in Section 1 test your knowledge in this area. Next, let's take a look at a practice question related to DOM.
DOM Practice Question
Of the following methods, select the one which has not been defined by the DOM Level 2 Node interface:
Option
- appendChild
- getElementsByTagName
- hasAttributes
- replaceChild
Answer
B
Commentary
The getElementsByTagName method in answer B is a method defined by the interface Document or Element.
Examples of Questions Appearing in Section 1 - SAX
Now, let's cover some important points related to SAX. First, let's confirm each interface/class defined in SAX. SAX 2.0 is made up of the following three packages (including SAX 2.0 Extensions).
- org.xml.sax
- org.xml.sax.helpers
- org.xml.sax.ext
The main interfaces/classes defined within these packages are shown in Table 1 (along with usage frequency):
Table 1: Main SAX 2.0 Interfaces / Classes and Usage FrequencyInterface / Class name | Frequency of Use |
---|---|
org.xml.sax.Attributes | High |
org.xml.sax.ContentHandler | High |
org.xml.sax.EntityResolver | Middle |
org.xml.sax.ErrorHandler | High |
org.xml.sax.Locator | Middle |
org.xml.sax.XMLFilter | Middle |
org.xml.sax.XMLReader | High |
org.xml.sax.helpers.AttributesImpl | Low |
org.xml.sax.helpers.DefaultHandler | High |
org.xml.sax.helpers.XMLFilterImpl | Low |
org.xml.sax.helpers.XMLReaderFactory | High |
org.xml.sax.ext.LexicalHandler | Low |
Interfaces / classes indicated as having a "High" usage frequency in Table 1 are the very basic interfaces / classes that always appear in normal SAX programs. Interfaces / classes with a "Middle" usage frequency are used according to need. Those with "Low" usage frequency are quite limited in use, as shown below. *3
*3 | With respect to other interfaces/classes, it is probably sufficient to have a general understanding, supplemented by a deeper understanding gained through practical work applications. This trend in questions applies not only to Section 1, but also to SAX-related questions in Sections 2 and 6. |
- Class AttributesImpl: Used to change attribute value or add new attribute when using interface org.xml.sax.XMLFilter to process an XML document.
- Class XMLFilterImpl: Use in conjunction with interface XMLFilter when you want to perform strong filtering.
- Interface LexicalHandler: Used for specific purposes (when you want to know whether a document type declaration exists within an XML document you wish to process, etc.)
JAXP is used quite frequently when using these interfaces/classes to do SAX programming in Java. However, there are no questions on the exam related to JAXP, for the same reasons explained in connection with DOM programming.
There are some significant differences between the procedures defined for SAX 2.0 and the procedures defined for JAXP. When an XML document is loaded according to SAX 2.0 procedures, the parser will provide support for XML namespaces by default (feature http://xml.org/sax/features/namespaces set to true). On the other hand, when an XML document is loaded according to JAXP procedures, the parser will not provide support for namespaces by default (feature http://xml.org/sax/features/namespaces set to false). Knowing these kinds of differences is not only important for the exam, but also for practical work applications.
Given the preceding, let's take a look at a practice question related to SAX that generally appears in Section 1:
SAX Practice Question
Select the statement that is correct as a method for setting LexicalHandler implementation class for interface org.xml.sax.XMLReader. Assume that the variable name that references the LexicalHandler implementation class instance is "lexHandlerImpl."
Option
- Execute setLexicalHandler(lexHandlerImpl); for XMLReader object prior to executing the parse method of XMLReader interface
- Execute setFeature("http://xml.org/sax/properties/lexical-handler", true); for XMLReader object prior to executing the parse method of XMLReader interface
- Execute setProperty("http://xml.org/sax/properties/lexical-handler", true); for XMLReader object prior to executing the parse method of XMLReader interface
- Execute setProperty("http://xml.org/sax/properties/lexical-handler", lexHandlerImpl); for XMLReader object prior to executing the parse method of XMLReader interface
Answer
D
Commentary
This question uses a handler setting for XMLReader interface as an example, asking for an understanding of overall property setting methods.
Use setProperty method to set properties in SAX programming. To call this method, designate the property name to be set in the first argument, and the value to be set in the second argument. As in the example, to set LexicalHandler, you designate "http://xml.org/sax/properties/lexical-handler" in the first argument of the method, and the LexicalHandler implementation class instance in the second argument. Accordingly, "D" is the correct answer.
There is no setLexicalHandler method (answer A). The setFeature method in answer B is a method used when setting features under SAX programming.
XML Tutorial - XML Master Professional Application Developer Edition Indexs