You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Barry Adams <ba...@magus.co.uk> on 2000/10/27 04:39:52 UTC
Xerces-J Parse throws NoSuchElementException
When using Xerces 1.2.1 (and also previously 1.0.4) as a non Validating
parser, i get a java.util.NoSuchElementException when it tries to read an
attribute, any idea why, I'm I calling it somehow wrong and if so why isn't
it throwing a SAXException?
Example input:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD
/wml_1.1.xml">
<wml>
<card title="fieldScores=3,title:1">
-- Error Thrown Here before rest of file read
Sax Invoked by
import java.io.*;
import java.util.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import org.apache.xerces.parsers.SAXParser;
public class XMLSaxHandler extends org.xml.sax.helpers.DefaultHandler {
public static String SAXParserName = "org.apache.xerces.parsers.SAXParser";
XMLCallBack whoToCall;
public StringBuffer content;
private Vector activeElements;
public XMLSaxHandler(XMLCallBack xcb) {
super();
whoToCall = xcb;
content = new StringBuffer();
activeElements = new Vector();
}
private void reset(){
content = new StringBuffer();
activeElements = new Vector();
}
public void readXML(InputStream byteStream) throws IOException {
reset();
try {
XMLReader xr = XMLReaderFactory.createXMLReader(SAXParserName);
xr.setContentHandler(this);
xr.setErrorHandler(this);
InputSource is = new InputSource(byteStream);
if (whoToCall instanceof Parser){
Parser p = (Parser) whoToCall;
is.setEncoding(p.characterEncoding);
}
try {
xr.setFeature("http://xml.org/sax/features/validation", false);
} catch (org.xml.sax.SAXException e) {
System.out.println("error in setting up parser feature");
}
xr.parse(is);
} catch (org.xml.sax.SAXException e){
e.printStackTrace();
throw new IOException("SAX Exception "+e);
}
}
public void characters(char c[], int start,int len){
System.err.println("- "+new String(c,start,len));
content.append(c,start,len);
}
public void ignorableWhitespace(){
content.append(" ");
}
public void endDocument(){
send();
}
public void startElement(String nameURI,String localname,String qname,
Attributes att){
if (content.length()!=0){send();}
String name = localname;
System.err.println("<"+nameURI+" "+localname+" "+qname+">");
if (name.equals("")){
name = qname;
}
XMLAttributeContext xac = new XMLAttributeContext();
activeElements.add(name);
for(int i=0;i<att.getLength();i++){
String aname = att.getLocalName(i);
if (aname.equals("")){
aname = att.getQName(i);
}
String value = att.getValue(i);
if (!value.equals("")){
whoToCall.attCallBackPass1(name+"!"+aname,value,xac);
}
}
for(int i=0;i<att.getLength();i++){
String aname = att.getLocalName(i);
if (aname.equals("")){
aname = att.getQName(i);
}
String value = att.getValue(i);
if (!value.equals("")){
whoToCall.attCallBackPass2(name+"!"+aname,value,xac);
}
}
}
public void endElement(String nameURI,String localname,String qname){
if (content.length()!=0){send();}
String name = localname;
if (name.equals("")){
name = qname;
}
activeElements.remove(name);
}
private void send(){
StringBuffer whatisit = new StringBuffer();
for(Enumeration e=activeElements.elements();e.hasMoreElements();){
String name = (String) e.nextElement();
whatisit.append("!");
whatisit.append(name);
}
whoToCall.callBack(whatisit.toString(),content.toString());
content.setLength(0);
}
}
Stack Trace was
java.util.NoSuchElementException
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:917)
at MyApp.XMLSaxHandler.readXML(XMLSaxHandler.java:41)
Changing the XML file, and checking output from StartElement and characters
output calls reveals the error occurs while reading an element with an
attribute.
Barry Adams
badams@magus.co.uk