Checking if element is empty/endelement using xmlstreamreader

Hi

When using XMLStreamReader to parse XML-data I got a problem. When a tag is both start AND end tag (for instance "<tag />") then the reader only recognize it as a XMLStreamConstants.START_ELEMENT. How can I check if this is also a end-element?

import java.io.*;

import javax.xml.stream.*;

publicclass Parser

{

publicstaticvoid main(String args[])

{

try

{

FileInputStream in =new FileInputStream("test.xml");

XMLInputFactory factory = XMLInputFactory.newInstance();

XMLStreamReader parser = factory.createXMLStreamReader(in);

int event;

while((event = parser.next()) != XMLStreamConstants.END_DOCUMENT)

{

switch(event)

{

case XMLStreamConstants.START_ELEMENT:

{

System.out.println("StartElement: " + parser.getLocalName());

}

break;

case XMLStreamConstants.END_ELEMENT:

{

System.out.println("EndElement: " + parser.getLocalName());

}

break;

}

}

}

catch(FileNotFoundException e)

{

e.printStackTrace();

}

catch(XMLStreamException e)

{

e.printStackTrace();

}

}

}

For the XMLTextReader in C#.NET there is a .isEmptyElement property that I can check, but I havent found any similar for Java.

[2488 byte] By [invictus2a] at [2007-11-27 6:51:08]
# 1

From the JavaDocs at http://java.sun.com/webservices/docs/1.5/api/javax/xml/stream/XMLStreamReader.html#next():

NOTE: empty element (such as <tag/>) will be reported with two separate events: START_ELEMENT, END_ELEMENT - This preserves parsing equivalency of empty element to <tag></tag>. This method will throw an IllegalStateException if it is called after hasNext() returns false.

So, it should be recognized as an END_ELEMENT as well. Are you saying that this is not the case?

kevjavaa at 2007-7-12 18:25:24 > top of Java-index,Java Essentials,New To Java...
# 2
No you are right. I must have missed something :( Thats how it goes when I am used to .NET.Thanks for pointing it out though.
invictus2a at 2007-7-12 18:25:24 > top of Java-index,Java Essentials,New To Java...
# 3
> No you are right. I must have missed something :(> Thats how it goes when I am used to .NET.> > Thanks for pointing it out though.No problem at all :). Glad I could help.
kevjavaa at 2007-7-12 18:25:24 > top of Java-index,Java Essentials,New To Java...