Java Reference
In-Depth Information
The argument to the newInstance() method is the constant from the XMLConstants class that defines
the URI for the Schema Definition Language. There's a static import statement for the static names in
this class, so you don't need to qualify the name of the constant. The Schema object is created either by
passing the non- null File reference schemaFile to the newSchema() method or by calling the newS-
chema() method with no argument. In both cases the Schema object that is created is passed to the setS-
chema() method for the parser factory object. The parser that is subsequently created by the SAXParser-
Factory object uses the schema encapsulated by the Schema object to validate documents. In this way
the program is able to process documents for which the schema is specified by hints in the document,
as well as documents for which the schema is specified independently through the second command-line
argument.
SUMMARY
In this chapter I discussed the fundamental characteristics of XML and how Java supports the analysis and
synthesis of XML documents. This is very much an introductory chapter on XML and only covers enough
of the topic for you to understand the basic facilities that you have in Java for processing XML documents.
In the next chapter you see how you can synthesize an XML document programmatically so you can write it
to a file. You also find out how you can read an XML document and use it to reconstitute Java class objects.
EXERCISES
You can download the source code for the examples in the topic and the solutions to the following exer-
cises from www.wrox.com .
1. Write a program using SAX that counts the number of occurrences of each element type in an
XML document and displays them. The document file to be processed should be identified by the first
command-line argument.
2. Modify the program resulting from the previous exercise so that it accepts optional additional
command-line arguments that are the names of elements. When there are two or more command-line
arguments, the program should count and report only on the elements identified by the second and
subsequent command-line arguments.
• WHAT YOU LEARNED IN THIS CHAPTER
TOPIC
CONCEPT
XML
XML is a language for describing data that is to be communicated from one computer to another. Data is
described in the form of text that contains the data, plus markup that defines the structure of the data.
XML is also a meta-language because you can use XML to create new languages for defining and struc-
turing data.
Search WWH ::




Custom Search