Hi,
I am writing the code where I get the exception. The exception occurs in makeDatGroup(). I have given a "e.printStacktrace" in the catch block of makeDataGroup(). To be more exact I think the error comes when we execute the "parser.parse(insource);" statement of the above said method.
import java.io.*;
import com.ibm.xml.parser.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource; // Required by DOM Parser
import com.ibm.xml.parsers.DOMParser;
import com.ibm.xml.parsers.NonValidatingDOMParser; // XML DOM Parser
/** class XMLDataGroupFactory
*
* @see makeDataGroup(
String buffer)
* @see putLine(String line)
* @see makeDataGroup()
* @since JDK 1.3
*/
public class XMLDataGroupFactory {
public XMLDataGroupFactory() {
buffer=new StringBuffer(200);
}
/** Add some information to the current buffer */
public boolean putLine(String line) {
// If we don't know the
doc name yet, establish the name
if (docName==null && line!=null && line.length()>2) {
docName=line.substring(1,line.length()-1);
endOfDocName="</"+docName+">";
}
buffer.append(line);
// Use a newline so we can see breaks in the source data for multi-line fields
// and also so we can return the line number for errors
buffer.append("\r\n");
/** Checks if this is the end of the document for an XML block and
if so it returns true. */
String trimmedline = line.trim();
return ( trimmedline.equals("</") | | trimmedline.equals(endOfDocName) );
}
/** Erase the current buffer and start over. Clears the last error. */
public void reset() {
buffer.setLength(0);
status=null;
docName=null;
endOfDocName=null;
}
/** Set the buffer directly, then parse the XML document in the buffer, and generate either
a DataGroup or an exception.
*/
public DataGroup makeDataGroup(String buffer) throws Exception {
reset();
putLine(buffer);
return makeDataGroup();
}
/** Parse the XML document in the buffer, and generate either a DataGroup or an exception. */
public DataGroup makeDataGroup() throws Exception {
// Copy off the data and clear the buffer we're in a clean state if we get an exception
String xmlData=new String(buffer);
reset();
// New IBM XML Parser from here ....
DOMParser parser = new DOMParser();
StringReader xmlSR = new StringReader(xmlData);
try {
InputSource insource = new InputSource(xmlSR); // Set input source to the StringReader
parser.parse(insource); // Parse it
}
catch (Exception e) {
e.printStackTrace();
}
Document doc = null;
doc = parser.getDocument(); // Results are a document
// .... to here New IBM XML Parser
xmlSR.close(); // Note that this could throw an IOException
if (doc==null | | doc.getDocumentElement()==null) {
String errorInfo;
if (status==null)
errorInfo="";
else
errorInfo=" "+status.getValue("message");
throw new Exception("Invalid XML document");
}
return parseNode(doc.getDocumentElement());
}
// Parse a node, and produce a dataGroup to represent that node
// old private static DataGroup parseNode(Node parentNode) {
private static DataGroup parseNode(Node node) {
// New IBM XML Parser here ....
DataGroup datagroup = new DataGroup(node.getNodeName());
NodeList children = node.getChildNodes();
if (children != null) {
int len = children.getLength();
for (int i = 0; i < len; i++) { // loop thru all children
if ( children.item(i).getNodeType() == Node.ELEMENT_NODE ) {
if (hasElementChildren(children.item(i))==true) { // if has children
datagroup.addDataGroup(parseNode(children.item(i))); // parse the child into a datagroup
}
else {
if ( children.item(i).hasChildNodes() == true ) { // Make sure there is data for this tag
datagroup.addValue(children.item(i).getNodeName(),children.item(i).getLastChild().getNodeValue());
}
// The above is with the assumption that the last child is a text node.
// If dante ever starts accepting other node types ( like comments ). This will have to be
// restructered
}
}
}
}
return datagroup;
// .... to here New IBM XML Parser
}
// Any node that has text will return true for hasChildren. This method tells you
// whether a node has any Element children.
private static boolean hasElementChildren(Node node) {
boolean hasChildren=false;
for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
int type=n.getNodeType();
if (type==node.ELEMENT_NODE) { // Skip the text elements
hasChildren=true;
break;
}
}
return hasChildren;
}
public DataGroup makeDataGroup(BufferedReader in) throws Exception {
String line;
reset();
boolean beginTagFound = false;
String beginTag = null;
while (true) {
line = null;
line=in.readLine();
if (line == null)
if (beginTagFound == false)
return null;
else
break;
if( !beginTagFound ) {
int index = line.indexOf('<');
if( index != -1 ) {
int endIndex = line.indexOf('>',index);
if( endIndex != -1 && endIndex>index+1 ) {
beginTag = line.substring(index+1, endIndex);
beginTagFound = true;
}
}
}
putLine(line);
//if (line.startsWith("</")) break;
if (line.equals("</") | | line.indexOf("</"+beginTag+">") != -1)
break;
}
return makeDataGroup();
}
/** Returns information about the last parsing error.
Returns null if there is no last error to report.
Note that the reset() method clears the last error. */
public DataGroup getLastError() {
return status;
}
private void setLastError(DataGroup err) {
status=err;
}
// Inner class for error handling
class ErrorHandler implements ErrorListener {
public ErrorHandler(XMLDataGroupFactory parent) {
this.parent=parent;
}
public int error(String fileName, int lineNo, int charOffset, Object key, String msg) {
DataGroup err=new DataGroup("ParserError");
//Don't return line-- it always will be one because of the way we concatenate
//everything into a single line
//err.setValue("line",lineNo);
err.setValue("character_offset",charOffset);
err.setValue("message",msg);
parent.setLastError(err);
return 1;
}
private XMLDataGroupFactory parent;
} // ErrorHandler inner class
private String endOfDocName=null;
private String docName=null;
private DataGroup status;
private StringBuffer buffer;
}
Thanks,
Aarthy