[JDEV] <x> xml in jabberbeans
Benjamin H Szekely
bhszekel at us.ibm.com
Wed Jul 11 12:40:37 CDT 2001
Here is the meat and potatoes of the serializing handler:
/**
* This is an exact copy of the start element in the main handler.
*
* @param name string that holds the element name
* @param attributes AttributeList of attributes going with this
element
* @exception SAXException thrown on error (unexpected element)
*/
public void handleStartElement(String name, AttributeList attributes)
throws SAXException
{
accumulated.append('<');
accumulated.append(name);
for (int i = 0; i<attributes.getLength();i++)
{
accumulated.append(' ');
accumulated.append(attributes.getName(i));
accumulated.append('=');
accumulated.append('\"');
accumulated.append(attributes.getValue(i));
accumulated.append('\"');
}
accumulated.append('>');
}
What if change to: (escaping the bad values)
/**
* This is an exact copy of the start element in the main handler.
*
* @param name string that holds the element name
* @param attributes AttributeList of attributes going with this
element
* @exception SAXException thrown on error (unexpected element)
*/
public void handleStartElement(String name, AttributeList attributes)
throws SAXException
{
accumulated.append('<');
accumulated.append(name);
for (int i = 0; i<attributes.getLength();i++)
{
accumulated.append(' ');
accumulated.append(XMLData.escapeString(attributes.getName(i)));
accumulated.append('=');
accumulated.append('\"');
accumulated.append(XMLData.escapeString(attributes.getValue(i)));
accumulated.append('\"');
}
accumulated.append('>');
}
We could make a similar modification in the charaters() function:
/**
* This is an exact copy of the characters function in the main
handler
*
* @param ch character string detected
* @param start start position
* @param length length of string
* @exception SAXException thrown on error
*/
public void characters(char[] ch, int start, int length)
throws SAXException
{
accumulated.append(XMLData.escapeString(new String(ch,start,length)));
}
All of this string manipulation seems very innefficient..I guess the lesson
of the day is try to use the sax parser to parse your xml...
Ben
More information about the JDev
mailing list