1
1
<?xml version='1.0' encoding='UTF-8'?>
3
* Copyright 2004-2006 The Apache Software Foundation.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
2
17
<!DOCTYPE faqs SYSTEM 'dtd/faqs.dtd'>
3
18
<faqs title='Programming with SAX'>
93
<faq title="Attributes and ContentHandler.startElement">
94
<q>Why is the Attributes parameter passed to startElement always a
95
reference to the same object?</q>
97
<p>Outside the scope of <code>startElement</code>, the value of the
98
<code>Attributes</code> parameter is undefined. For each instance of Xerces'
99
SAX parser, there exists only one <code>Attributes</code> instance which
100
is reused for every new set of attributes. Before each
101
<code>startElement</code> callback, the previous values in this object
102
will be overwritten. This is done for performance reasons in order
103
to reduce object creation. To persist a set of attributes
104
beyond <code>startElement</code> the object should be cloned, for
105
instance by using <code>org.xml.sax.helpers.AttributesImpl</code>.
77
110
<faq title="Namespace of xmlns attributes">
78
111
<q>Why does the SAX parser report that xmlns attributes have no namespace?</q>
80
<p>An erratum for the Namespaces in XML recommendation put namespace declaration
81
attributes in the namespace "http://www.w3.org/2000/xmlns/". SAX2 (SAX 2.0.1)
82
does not agree with this change so conforming parsers must report that these
83
attributes have no namespace. Xerces behaves according to SAX2. Your code
84
must handle this discrepancy when interacting with APIs such as DOM and
85
applications which expect a namespace for xmlns attributes.</p>
113
<p>An erratum for the Namespaces in XML Recommendation put namespace declaration
114
attributes in the namespace "http://www.w3.org/2000/xmlns/". By default,
115
SAX2 (SAX 2.0.2) follows the original Namespaces in XML Recommendation, so
116
conforming parsers must report that these attributes have no namespace. To
117
configure the parser to report a namespace for such attributes, turn on
118
the <link idref='features' anchor='xmlns-uris'>xmlns-uris</link> feature.
120
<p>When using Xerces 2.6.2 (or prior) or other parser implementations
121
that do not support this feature, your code must handle this discrepancy
122
when interacting with APIs such as DOM and applications which expect a namespace
123
for xmlns attributes.
89
<faq title="Encodings and XML Version Via SAX">
128
<faq title="Encodings and XML Version via SAX">
90
129
<q>Is there any way I can determine what encoding an entity was
91
130
written in, or what XML version the document conformed to, if I'm
94
<p>The answer to this question is that, yes there is a way, but it's
95
not particularly beautiful. There is no way in SAX 2.0.0 or
96
2.0.1 to get hold of these pieces of information; the SAX
97
Locator2 interface from the 1.1 extensions--still in Beta at
98
the time of writing--does provide methods to accomplish this,
99
but since Xerces is required to support precisely SAX 2.0.1 by
100
Sun TCK rules, we cannot ship this interface. However, we can
101
still support the appropriate methods on the objects we
102
provide to implement the SAX Locator interface. Therefore,
103
assuming <code>Locator</code> is an instance of the SAX
104
Locator interface that Xerces has passed back in a
105
<code>setDocumentLocator</code> call,
106
you can use a method like this to determine the encoding of
107
the entity currently being parsed:
109
<source>import java.lang.reflect.Method;
110
private String getEncoding(Locator locator) {
111
String encoding = null;
112
Method getEncoding = null;
115
locator.getClass().getMethod("getEncoding", new Class[]{});
116
if(getEncoding != null) {
117
encoding = (String)getEncoding.invoke(locator, null);
119
} catch (Exception e) {
120
// either this locator object doesn't have this
121
// method, or we're on an old JDK
125
<p>This code has the advantage that it will compile on JDK
126
1.1.8, though it will only produce non-null results on 1.2.x
127
JDK's and later. Substituting <code>getXMLVersion</code> for
128
<code>getEncoding</code> will enable you to determine the
129
version of XML to which the instance document conforms.
133
<p>Yes. As of SAX 2.0.2 encoding and version information is made
134
available through the <code>org.xml.sax.ext.Locator2</code>
135
interface. In Xerces, instances of the SAX <code>Locator</code> interface
136
passed to a <code>setDocumentLocator</code> call will also implement
137
the <code>Locator2</code> interface. You can determine the encoding
138
and XML version of the entity currently being parsed by calling the
139
<code>getEncoding()</code> and <code>getXMLVersion()</code> methods.