parsing an empty element with SAX
843834Jun 2 2005 — edited Jun 3 2005Hallo,
I'm trying to process an xml file, and simply output the data as a comma delimited text file. Using SAX, I can get the output i want.
the only thing is, I have some empty elements in my xml. For these empty element, I want to print something like "empty" in my comma delimited output file. However, my code simply skips any empty elements and outputs nothing. does anyone know how i can get my code to recognise an empty element and then print "empty" ?
I'm using Xerces2, and the code i have is:
import java.io.FileReader;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
public class MySAXApp extends DefaultHandler
{
public static void main (String args[])
throws Exception
{
XMLReader xr = XMLReaderFactory.createXMLReader();
MySAXApp handler = new MySAXApp();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
// Parse each file provided on the
// command line.
for (int i = 0; i < args.length; i++) {
FileReader r = new FileReader(args);
xr.parse(new InputSource(r));
}
}
public MySAXApp ()
{
super();
}
////////////////////////////////////////////////////////////////////
// Event handlers.
////////////////////////////////////////////////////////////////////
public void startDocument ()
{
}
public void endDocument ()
{
}
public void startElement (String uri, String name,
String qName, Attributes atts)
{
}
public void endElement (String uri, String name, String qName)
{
}
public void characters (char ch[], int start, int length)
{
System.out.println();
if (length == 0 || ch==null || start == length)
{ System.out.println("empty");
}
for (int i = start; i < start + length; i++)
{ System.out.print(ch[i]);
}
System.out.println("start = " + start);
System.out.println("ch.length = " + ch.length);
}
}