Re: SAX and Java 6
Mon, 25 Feb 2008 19:59:11 +0200, /Stanimir Stamenkov/:
Mon, 25 Feb 2008 07:47:31 -0800 (PST), /Ghislain/:
Step by step I ran the reader and discovered that the with
JDK 6 the XMLreader doesn't read properly a node's attributes.
Your example is not sufficient - I have no problem reading element's
attributes using SAX and Java 6.
Try the following example with any "test.xml" put into the same
package as the class:
-----Xml2Office.java
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class Xml2Office extends DefaultHandler {
Xml2Office() {
//super();
}
public static void main(String[] args) throws Exception {
String uri = Xml2Office.class
.getResource("test.xml").toExternalForm();
InputSource input = new InputSource(uri);
XMLReader xr = XMLReaderFactory.createXMLReader();
DefaultHandler handler = new Xml2Office();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
xr.parse(input);
}
public void startElement(String uri, String localName,
String qname, Attributes atts) {
for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
System.out.print("<" + qname);
for (int i = 0, len = atts.getLength(); i < len; i++) {
System.out.print(" " + atts.getQName(i) + "=\""
+ atts.getValue(i) + "\"");
}
System.out.println(">");
indent++;
}
public void endElement(String uri, String localName,
String name) {
indent--;
}
private int indent;
}
-----Xml2Office.java--
--
Stanimir