Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

parsing an empty element with SAX

843834Jun 2 2005 — edited Jun 3 2005
Hallo,

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);

}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 1 2005
Added on Jun 2 2005
2 comments
483 views