XML Tutorial - XML Master Professional Application Developer Edition
Volume 7 : The Greatest Challenge "Integrated Questions"
Tatsuya Kimura
Scope of Questions Appearing in Section 6
Questions appearing in this section are integrated questions that test the knowledge required when using a combination of the following elemental technologies:
- XML
- XML namespaces
- DTD, XML Schema
- XSLT
- DOM, SAX
The processes of these questions are frequently used in actual practice, such as the following:
- Questions asking the results of using DOM or SAX to process XML documents conforming to some type of XML schema
- Questions asking the results of applying an XSLT stylesheet to an XML document incorporating practical business data
- Questions asking whether an XML document subject to some type of processing is in accordance with a certain XML specification (XML schema written in DTD or XML Schema)
- Questions asking the results of processing an XML document that uses XML namespaces
To deal with this kind of practical scenario, the XML documents and Java programs introduced in this section are comparatively lengthy. A certain amount of time is necessary to accurately comprehend the XML documents/Java programs presented. One important strategy for taking the exam is to leave enough time for Section 6.
Now, let's take a look at a practice question and associated commentary. This question will ask the result of using DOM to process an XML document following XML schema written in ContactXML.
Example of Questions Appearing in Section 6
Select the answer that correctly describes the output result (println method output result) when processing an XML document following [DTD] below, using the method shown in [Processing by DOM].
[DTD]
<?xml version='1.0' encoding='UTF-8' ?>
<!ENTITY % unnamed "http://www.xmlns.org/2002/ContactXML">
<!ELEMENT ContactXML (ContactXMLItem+)>
<!ATTLIST ContactXML version(1.1)#REQUIRED
creator CDATA #IMPLIED
xmlns CDATA #FIXED 'http://www.xmlns.org/2002/ContactXML'>
<!ELEMENT ContactXMLItem (PersonName, Extension?)>
<!ATTLIST ContactXMLItem lastModifiedDate CDATA #IMPLIED>
<!ELEMENT PersonName (PersonNameItem+)>
<!ELEMENT PersonNameItem (FullName, FirstName?, LastName?)>
<!ATTLIST PersonNameItem xml:lang CDATA #REQUIRED>
<!ELEMENT FullName (#PCDATA)>
<!ATTLIST FullName pronunciation CDATA #IMPLIED>
<!ELEMENT FirstName (#PCDATA)>
<!ATTLIST FirstName pronunciation CDATA #IMPLIED>
<!ELEMENT LastName (#PCDATA)>
<!ATTLIST LastName pronunciation CDATA #IMPLIED>
<!ELEMENT Extension (ExtensionItem+)>
<!ELEMENT ExtensionItem (#PCDATA)>
[Conditions for the XML document subject to processing]
Follows [DTD] definitions
・No document type declaration
・Root element name (document element name) is "ContactXML"
・Consists of elements, attributes and character data;
no comments or CDATA sections included
・Indents (line returns, tabs, and other ignorable whitespace) are
not included
[Processing by DOM]
The XML document is processed by calling method processXML, as shown below.
processXML(doc);
At this point, variable doc references the org.w3c.dom.Document instance of the loaded XML document. The DOM parser is namespace aware. There are no compile errors.
The implementation of processXML method is as follows:
public static void processXML(Document doc) {
Node root = doc.getDocumentElement();
NodeList list1 = root.getChildNodes();
for(int i = 0; i < list1.getLength(); i++) {
NodeList list2 = list1.item(i).getChildNodes();
for(int j = 0; j < list2.getLength(); j++) {
NodeList list3 = list2.item(j).getChildNodes();
for(int k = 0; k < list3.getLength(); k++) {
NodeList list4 = list3.item(k).getChildNodes();
for(int l = 0; l < list4.getLength(); l++) {
System.out.println(list4.item(l).getNodeValue());
}
}
}
}
}
Option
- A runtime exception always occurs
- A runtime exception occurs when the FirstName element is not coded in the XML document
- A runtime exception does not occur; however, the content text string for FullName element is not output
- A runtime exception does not occur; content text string for FullName element is output
Answer
C
Commentary
To answer this question, you must first read and understand [DTD]. There is quite a bit of code involved for [DTD] in this practice question; it's probably best to extract just the element type declaration first, rather than try to read through the entire definition. Extracting out the element type declaration from [DTD] shows us the following:
<!ELEMENT ContactXML (ContactXMLItem+)>
<!ELEMENT ContactXMLItem (PersonName, Extension?)>
<!ELEMENT PersonName (PersonNameItem+)>
<!ELEMENT PersonNameItem (FullName, FirstName?, LastName?)>
<!ELEMENT FullName (#PCDATA)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
<!ELEMENT Extension (ExtensionItem+)>
<!ELEMENT ExtensionItem (#PCDATA)>
To work through the rest of the question, you may decipher other attribute list declarations as necessary based on the above. However, you probably noticed that [Processing by DOM] doesn't include a process for XML attribute operations in the coding for processXML method. Accordingly, you may ignore the attribute list declaration in this question (there aren't any possible answers that deal with attribute processing).
The parameter entity ("unnamed") in line 2 of [DTD] is not actually referenced from any other location, and so may also be ignored.
Now, let's take a close look at line 6 of [DTD]. Here, a value "http://www.xmlns.org/2002/ContactXML" is coded as an attribute fixed value; this is equivalent to an XML document namespace designation.
An example of an XML document (ignore attribute list declaration) following the element type declaration of [DTD] is as follows:
<ContactXML xmlns="http://www.xmlns.org/2002/ContactXML"> [R]
<ContactXMLItem> [+]
<PersonName> [1]
<PersonNameItem> [+]
<FullName>Jenny Smith</FullName> [1]
<FirstName>Jenny</FirstName> [?]
<LastName>Smith</LastName> [?]
</PersonNameItem>
</PersonName>
<Extension> [?]
<ExtensionItem>Extension Information</ExtensionItem> [+]
</Extension>
</ContactXMLItem>
</ContactXML>
[R] represents the root element; [+], [1], and [?] represent the number of occurrences for the element designated in [DTD]. As shown in [Conditions for the XML document subject to processing], no indents are included in the XML document to be processed.
Based on the preceding, we can work through processXML method as follows:
The first process obtains the root element of the XML document, storing it in variable root. Next, four variables (variables list1 through list4) are used to pick up the child nodes in order from the root element, ultimately outputting the node value (nodeValue) stored in variable list4.
Let's make sure we understand specifically which nodes are stored in variables list1 through list4.
- list1 variable: root element child node (ContactXMLItem element)
- list2 variable: ContactXMLItem element child node (PersonName element or Extension element)
- list3 variable: PersonName element (or Extension element) child node (PersonNameItem element or ExtensionItem element)
- list4 variable: PersonNameItem element (or ExtensionItem element) child node (element FullName/FirstName/LastName in the case of PersonNameItem; text node in the case of ExtensionItem element)
Accordingly, the node stored in the list4 variable is one of the following:
- FullName element
- FirstName element
- LastName element
- Text Node held by ExtensionItem element
What results do we get from obtaining the values (nodeValue) for these elements and text nodes (in other words, executing getNodeValue method)? Actually, when executing getNodeValue method for the elements, null will always be returned. Under DOM, elements have no direct value, while the text node (child node of the elements) is what holds the value.
Accordingly, only in the case where we execute getNodeValue method for the text nodes do we obtain a value other than null.
Given the preceding, let's look at the possible answers to this question.
- Option A: Null is returned when executing getNodeValue method for the elements, while the text value is obtained when executing for text nodes. Accordingly, no runtime exception will occur. As such, Option A is incorrect.
- Option B: Regardless as to whether element FirstName is coded in the XML document, all for statements in processXML method execute normally; Option B is incorrect
- Option D: Executing getNodeValue method for the elements will not acquire a value; Option D is incorrect.
Accordingly, Option C is the correct answer.
This is a question about XML in which the XML document is not provided. In other words, you must read through the Java program (Processing by DOM) while imagining different outcomes, conceiving the XML document based on the XML schema provided in your own head. This might be a difficult task to perform within the time constraints of this exam, but this conceptual process is very much like what we programmers deal with in actual practice.
* * *
As you can see, the "XML Master Professional Application Developer" certification exam is more than simply a test of XML. Programmers who have used XML in various applications might have an edge in success. In other words, the major factor in passing the "XML Master Professional Application Developer" certification exam is actual practice using XML.
We have reviewed the major points for study related to the "XML Master Professional Application Developer" certification exam over seven different volumes. The exam leans heavily toward practical application. A frequent comment back from exam takers is that, despite the difficulty, the exam itself is very interesting. I encourage you to approach the exam with a sense of fun.
XML Tutorial - XML Master Professional Application Developer Edition Indexs