I found that the original code has problems with comments in the XML. So I've written some fixes that handle them. The changes follow.
in the main constructor:
Code:
public myXML(BufferedReader in) throws myXMLException, myXMLEncodingException, IOException {
myFileReader reader = new myFileReader(in);
try {
while (this.getTag() == null && reader.ready()) {
// continue until we have a valid root tag or run out of file content
readXML(this, reader);
}
}
catch (RuntimeException e) {
System.err.println("myXML exception "+e);
e.printStackTrace();
throw e;
}
if (debug) System.out.println("XML file successfully loaded");
}
additional method in myFileReader:
Code:
boolean ready() throws IOException {
return reader.ready();
}
the START_TAG case in readXML(myXML xml,myFileReader in):
Code:
case START_TAG:
if (debug) System.out.println("readXML: state START_TAG, c="+(char)ch);
state = popState(stack);
if (ch == '/') {
stack.push(new Integer(state));
state = CLOSE_TAG;
}
else if (ch == '?') {
state = DOCTYPE;
}
else if (xml.getTag() != null) {
in.unread(ch);
in.unread('<');
myXML child = xml.addElement(null);
readXML(child, in);
if (child.getTag() == null) {
xml.removeElement(child);
}
state = popState(stack);
}
else {
stack.push(new Integer(state));
state = OPEN_TAG;
sb.append((char)ch);
}
break;
the COMMENT case in readXML(myXML xml,myFileReader in):
Code:
case COMMENT: // Inside <!-- ... -->
if (debug) System.out.println("readXML: state COMMENT, c="+(char)ch);
if (ch == '>' && sb.toString().endsWith("--")) { // found the end of a comment
sb.setLength(0);
state = popState(stack);
if (debug) System.out.println("readXML: state COMMENT - Comment complete...returning");
return;
}
sb.append((char)ch);
break;
Hope this helps someone make more use of this very handy tool.
Ben