Java Reference
In-Depth Information
Start element: local name: zip qname: zip uri:
Characters: 60603
End element: local name: zip qname: zip uri:
Ignorable whitespace:
End element: local name: address qname: address uri:
End document:
We can see that with a DTD, the ignorable whitespace is recognized as such, and is passed to our
ignorableWhitespace() method. Although the parser is validating the XML, we can't be confident
that the document is valid. If any errors are found, the default do-nothing error-handling methods
inherited from the DefaultHandler class will be called. We can fix this quite easily by modifying our
MySAXHandler class, but let's first look at processing some other XML document flavors.
Processing a Document with Namespaces
We can convert our Address.xml file to use a namespace by modifying the root element like this:
<address xmlns="C:/Beg Java Stuff/AddressNamespace">
With this change to the XML file, the URI for the default namespace is
C:/Beg Java Stuff/AddressNamespace . This doesn't really exist, but it doesn't need to. It's just a
unique qualifier for the names within the namespace.
We will also need to use a different DTD that takes account of the use of a namespace so we must
modify the DOCTYPE declaration in the document:
<!DOCTYPE address SYSTEM "AddressNamespaceDoc.dtd">
You can now save the revised XML document with the name AddressNamespace.xml .
We must also create the new DTD. This is the same as the previous one with the addition of a
declaration for the xmlns attribute for the <address> element:
<!ATTLIST address xmlns CDATA #IMPLIED>
If you run the previous example with this version of the XML document, you should see the URI in the
output. Since the namespace is the default, there is no prefix name so the values for the localname
and qname parameters to the startElement() and endElement() methods are the same.
Using Qualified Names
We can change the document to make explicit use of the namespace prefix like this:
<?xml version="1.0"?>
<!DOCTYPE addresses:address SYSTEM "AddressNamespaceDoc.dtd">
<addresses:address xmlns:addresses="D:/Beg Java Stuff/AddressNamespace">
<addresses:buildingnumber> 29 </addresses:buildingnumber>
<addresses:street> South Lasalle Street</addresses:street>
Search WWH ::




Custom Search