Hi there,
I'm working with a client/server application and using SaxParser for reading in xml. I get the SaxParserException: XML document structures must start and end within the same entity. I understand what that means, but it isn't applicable! The xml data being used is well-formed. I checked the well-formedness with Stylus Studio to make sure. Here's the data:
<?xml version='1.0' encoding='UTF-8'?>
<vcmessage>
<vcsource>3</vcsource>
<processevent>16</processevent>
<shape>
<llindex>0</llindex>
<shapetype>9</shapetype>
<shapeproperties>
<shapelocation>
<xcoord>54</xcoord>
<ycoord>184</ycoord>
</shapelocation>
<bounds>
<width>24</width>
<height>24</height>
</bounds>
<fgcolor>
<fgred>0</fgred>
<fggreen>0</fggreen>
<fgblue>0</fgblue>
</fgcolor>
<bgcolor>
<bgred>255</bgred>
<bggreen>255</bggreen>
<bgblue>255</bgblue>
</bgcolor>
<thickness>1</thickness>
<isfilled>false</isfilled>
</shapeproperties>
</shape>
</vcmessage>
The parser generally stops around the </bgcolor> tag.
I'm using Eclypse as my IDE. I'm wondering if there's something wrong with it? Or maybe there's something wrong with the class I'm using for reading in the XML? Followng is the class.
Please advise,
Alan
package vcclient;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
public class XMLDocumentReader extends DefaultHandler
{
private VCClient client = null;
private Writer out;
private String lineEnd = System.getProperty("line.separator");
private boolean haveSourceType = false;
private boolean haveUserName = false;
private boolean haveMessage = false;
private boolean haveProcessEvent = false;
private boolean haveLinkedListIndex = false;
private boolean haveOpeningShapePropertiesTag = false;
private boolean haveShapeType = false;
private boolean haveOpeningShapeLocationTag = false;
private boolean haveShapeLocation = false;
private boolean haveOpeningXCoordTag = false;
private boolean haveOpeningYCoordTag = false;
private boolean haveOpeningBoundsTag = false;
private boolean haveBoundsWidth = false;
private boolean haveBoundsHeight = false;
private boolean haveOpeningFGColorTag = false;
private boolean haveOpeningBGColorTag = false;
private boolean haveOpeningThicknessTag = false;
private boolean haveOpeningIsFilledTag = false;
private boolean haveOpeningImageDataTag = false;
private boolean haveOpeningTextDataTag = false;
private boolean haveFGRed = false;
private boolean haveFGGreen = false;
private boolean haveFGBlue = false;
private boolean haveBGRed = false;
private boolean haveBGGreen = false;
private boolean haveBGBlue = false;
private boolean haveThickness = false;
private boolean haveIsFilled = false;
private boolean haveImageData = false;
private boolean haveTextData = false;
private VCMessage vcmessage = null;
public XMLDocumentReader(VCClient value)
{
client = value;
vcmessage = new VCMessage();
}
public VCMessage getVCMessage()
{
return vcmessage;
}
public boolean haveSourceType()
{
return haveSourceType;
}
public boolean ParseXML(InputStream stream)
{
boolean success = false;
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
// Set up output stream
out = new OutputStreamWriter(System.out, "UTF-8");
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( stream, this );
success = true;
}
catch (SAXParseException spe)
{
// Error generated by the parser
System.out.println("\n** Parsing error"
+ ", line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage() );
// Unpack the delivered exception to get the exception it contains
Exception x = spe;
if (spe.getException() != null)
x = spe.getException();
x.printStackTrace();
return success;
}
catch (SAXException sxe)
{
// Error generated by this application
// (or a parser-initialization error)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
return success;
}
catch (ParserConfigurationException pce)
{
// Parser with specified options can't be built
pce.printStackTrace();
return success;
}
catch (Throwable t)
{
t.printStackTrace();
return success;
}
return success;
}
public void startDocument()throws SAXException
{
emit("<?xml version='1.0' encoding='UTF-8'?>");
nl();
}
public void endDocument()throws SAXException
{
try {
nl();
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
public void startElement(String namespaceURI,
String lName, // local name
String qName, // qualified name
Attributes attrs)throws SAXException
{
String eName = lName; // element name
if (eName.equals(""))
eName = qName; // namespaceAware = false
emit("<"+eName);
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i); // Attr name
if (aName.equals("")) aName = attrs.getQName(i);
emit(" ");
emit(aName + "=\"" + attrs.getValue(i) + "\"");
}
}
emit(">");
if(makeStartTag(eName).equals(Constants.OPENING_SHAPEPROPERTIES))
haveOpeningShapePropertiesTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_SHAPELOCATION))
haveOpeningShapeLocationTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_BOUNDS))
haveOpeningBoundsTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_FGCOLOR))
haveOpeningFGColorTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_BGCOLOR))
haveOpeningBGColorTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_BGGREEN))
System.out.println("See BGGreen");
else if(makeStartTag(eName).equals(Constants.OPENING_BGBLUE))
System.out.println("See BGBlue");
else if(makeStartTag(eName).equals(Constants.OPENING_THICKNESS))
haveOpeningThicknessTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_ISFILLED))
haveOpeningIsFilledTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_IMAGEDATA))
haveOpeningImageDataTag = true;
else if(makeStartTag(eName).equals(Constants.OPENING_TEXTDATA))
haveOpeningTextDataTag = true;
}
public void endElement(String namespaceURI,
String sName, // simple name
String qName // qualified name
)throws SAXException
{
if(sName.equals("") && !qName.equals(""))
{
sName = qName;
emit("</"+sName+">");
}
else
emit("</"+sName+">");
if(makeEndTag(sName).equals(Constants.CLOSING_SOURCE_TYPE))
haveSourceType = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_USER))
haveUserName = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_MESSAGE))
haveMessage = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_PROCESSEVENT))
haveProcessEvent = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_LINKEDLISTINDEX))
haveLinkedListIndex = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_SHAPETYPE))
haveShapeType = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_SHAPELOCATION))
haveOpeningShapeLocationTag = false;
else if(makeEndTag(sName).equals(Constants.CLOSING_WIDTH))
haveBoundsWidth = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_HEIGHT))
haveBoundsHeight = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_BOUNDS))
haveOpeningBoundsTag = false;
else if(makeEndTag(sName).equals(Constants.CLOSING_FGRED))
haveFGRed = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_FGGREEN))
haveFGGreen = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_FGBLUE))
haveFGBlue = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_FGCOLOR))
haveOpeningFGColorTag = false;
else if(makeEndTag(sName).equals(Constants.CLOSING_BGRED))
haveBGRed = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_BGGREEN))
haveBGGreen = true;
else if(makeEndTag(sName).equals(Constants.CLOSING_BGBLUE))
{
System.out.println("See closing BGBlue");
haveBGBlue = true;
}
else if(makeEndTag(sName).equals(Constants.CLOSING_BGCOLOR))
haveOpeningBGColorTag = false;
else if(makeEndTag(sName).equals(Constants.CLOSING_THICKNESS))
{
System.out.println("XMLDocumentReader: Step2");
haveOpeningThicknessTag = false;
}
else if(makeEndTag(sName).equals(Constants.CLOSING_ISFILLED))
haveOpeningIsFilledTag = false;
else if(makeEndTag(sName).equals(Constants.CLOSING_IMAGEDATA))
haveOpeningImageDataTag = false;
else if(makeEndTag(sName).equals(Constants.CLOSING_TEXTDATA))
haveOpeningTextDataTag = false;
}
private String makeStartTag(String tag_name)
{
String start = "<";
String end = ">";
return start.concat(tag_name).concat(end);
}
private String makeEndTag(String tag_name)
{
String start = "</";
String end = ">";
return start.concat(tag_name).concat(end);
}
public void characters(char buf[], int offset, int len)throws SAXException
{
String s = new String(buf, offset, len);
if(haveSourceType == false)
{
if(vcmessage.getSourceType() == null)
{
try
{
if(s.equals(""))return;
int sourcetype = Integer.parseInt(s);
vcmessage.setSourceType(sourcetype);
}
catch(NumberFormatException nfe){}
}
}
else if(vcmessage.getSourceType() == SourceType.CHAT_SOURCE)
{
if(vcmessage.getSourceType() == SourceType.CHAT_SOURCE && haveUserName == false)
{
vcmessage.setUserName(s);
}
else if(vcmessage.getSourceType() == SourceType.CHAT_SOURCE && haveMessage == false)
{
//When the parser encounters interpreted characters like: & or <,
//then this method gets invoked more than once for the whole message.
//Therefore, we need to concatonate each portion of the message. The
//following method call automatically concatonates.
vcmessage.concatMessage(s);
}
}
else if(vcmessage.getSourceType() == SourceType.WHITEBOARD_SOURCE)
{
if(haveProcessEvent == false)
{
try
{
vcmessage.setProcessEvent(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveLinkedListIndex == false)
{
try
{
vcmessage.setLinkedListIndex(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveShapeType == false)
{
try
{
vcmessage.setShapeType(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
if(haveOpeningShapePropertiesTag)
{
if(haveOpeningShapeLocationTag)
{
if(haveOpeningXCoordTag)
{
try
{
vcmessage.setXCoordinate(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveOpeningYCoordTag)
{
try
{
vcmessage.setYCoordinate(Integer.parseInt(s));
//reset all flags for ShapeLocation, X and Y coordinates
haveOpeningXCoordTag = false;
haveOpeningYCoordTag = false;
//haveOpeningShapeLocationTag = false;
}
catch(NumberFormatException nfe){}
}
}
else if(haveOpeningBoundsTag)
{
if(haveBoundsWidth == false)
{
try
{
vcmessage.setBoundsWidth(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveBoundsHeight == false)
{
try
{
vcmessage.setBoundsHeight(Integer.parseInt(s));
//reset flag
//haveOpeningBoundsTag = false;
}
catch(NumberFormatException nfe){}
}
}
else if(haveOpeningFGColorTag)
{
if(haveFGRed == false)
{
try
{
vcmessage.setFGRed(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveFGGreen == false)
{
try
{
vcmessage.setFGGreen(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveFGBlue == false)
{
try
{
vcmessage.setFGBlue(Integer.parseInt(s));
//reset flag
//haveOpeningFGColorTag = false;
}
catch(NumberFormatException nfe){}
}
}
else if(haveOpeningBGColorTag)
{
if(haveBGRed == false)
{
try
{
vcmessage.setBGRed(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveBGGreen == false)
{
try
{
vcmessage.setBGGreen(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveBGBlue == false)
{ System.out.println("getting BGBlue data");
try
{
vcmessage.setBGBlue(Integer.parseInt(s));
//reset flag
//haveOpeningBGColorTag = false;
}
catch(NumberFormatException nfe){}
}
}
else if(haveOpeningThicknessTag)
{
try
{
vcmessage.setThickness(Integer.parseInt(s));
}
catch(NumberFormatException nfe){}
}
else if(haveOpeningIsFilledTag)
{
vcmessage.setIsFilled(s);
}
else if(haveOpeningImageDataTag && vcmessage.getProcessEvent() == org.jcanvas.comm.ProcessEvent.MODIFY)
{
vcmessage.setBase64ImageData(s);
}
else if(haveOpeningTextDataTag && vcmessage.getProcessEvent() == org.jcanvas.comm.ProcessEvent.MODIFY)
{
vcmessage.setTextData(s);
}
//reset
haveOpeningShapePropertiesTag = false;
}
}
emit(s);
}
//===========================================================
// Utility Methods ...
//===========================================================
// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void emit(String s)throws SAXException
{
try {
out.write(s);
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
// Start a new line
private void nl()throws SAXException
{
try {
out.write(lineEnd);
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
//treat validation errors as fatal
public void error(SAXParseException e)
throws SAXParseException
{
throw e;
}
// dump warnings too
public void warning(SAXParseException err)
throws SAXParseException
{
System.out.println("** Warning"
+ ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
}
}