I receive XML files that need parsing. I code in java regularly, so java SAX was my natural first choice. The XML files have a combination of text elements and one binary element (.xls file).
My parser handler is as:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException{
if(qName.equalsIgnoreCase("To")){
toFlag = true;
}
if(qName.equalsIgnoreCase("Subject")){
subjectFlag = true;
}
if(qName.equalsIgnoreCase("OutDocumentId")){
outdocmentIdFlag = true;
}
if(qName.equalsIgnoreCase("Filename")){
filenameFlag = true;
}
if(qName.equalsIgnoreCase("EmailType")){
emailTypeFlag = true;
}
if(qName.equalsIgnoreCase("Context")){
contextTypeFlag = true;
}
if(qName.equalsIgnoreCase("Blob")){
blobTypeFlag = true;
}
}
And the element data is parsed here:
public void characters(char ch[], int start, int length) throws SAXException{
String text = null;
if (toFlag) {
text = new String(ch, start, length);
getRequest().setRecipientEmail(text);
toFlag = false;
}
if (subjectFlag) {
text = new String(ch, start, length);
getRequest().setSubject(text);
subjectFlag = false;
}
if (outdocmentIdFlag) {
text = new String(ch, start, length);
getRequest().setOutDocId(text);
outdocmentIdFlag = false;
}
if (filenameFlag) {
text = new String(ch, start, length);
getRequest().setFilename(text);
filenameFlag = false;
}
if(emailTypeFlag) {
text = new String(ch, start, length);
getRequest().setEmailType(Integer.parseInt(text));
emailTypeFlag = false;
}
if(contextTypeFlag) {
text = new String(ch, start, length);
getRequest().setContext(text);
contextTypeFlag = false;
}
if(blobTypeFlag) {
text = new String(ch, start, length);
try {
getRequest().setBlob(Hibernate.createBlob(text.getBytes("UTF-16")));
} catch (UnsupportedEncodingException e) {
System.out.println("Error creating blob");
e.printStackTrace();
}
blobTypeFlag = false;
}
}
}
The problem is with the blob element, its being read in as a char[] (which I believe is incorrect ) ... because that's what they parent class allow to override during event processing.
Does anybody know how to use the SAX parse when one element, is not text but binary instead?
Greatly appreciated