Newline From SAX Parser

After working with SAX successfully for some time, we bumped into a production problem. It appears our Xerces implementation is adding whitespace and a new line to a data element in the string we get in the characters function in our content impl. There appears to be no whitespace in the element data in the XML file. Using "trim" resolves the issue but it's odd that the code didn't do that previously.

Has anyone seen added newlines from SAX?

[460 byte] By [hundela] at [2007-11-27 11:25:07]
# 1

I've never used sax (only dom), post a sample piece of xml you're parsing and the code that reads it, and maybe I can help.

hunter9000a at 2007-7-29 16:02:32 > top of Java-index,Java Essentials,Java Programming...
# 2

> After working with SAX successfully for some time, we

> bumped into a production problem. It appears our

> Xerces implementation is adding whitespace and a new

> line to a data element in the string we get in the

> characters function in our content impl. There

> appears to be no whitespace in the element data in

> the XML file. Using "trim" resolves the issue but

> it's odd that the code didn't do that previously.

>

> Has anyone seen added newlines from SAX?

Post a bit of the offending XML that you say has no white space or new lines.

sabre150a at 2007-7-29 16:02:32 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for your interest. Here is the source, parsing code, and result:

Info:SAXParser endElement chars:'\n0'

<PurchaseOrderChange>

<Header>

<OrderHeader>

<PurchaseOrderNumber>4810462392</PurchaseOrderNumber>

</OrderHeader>

<Summary>

<TotalLineItemNumber>0</TotalLineItemNumber>

</Summary>

</PurchaseOrderChange>

public synchronized void endElement(String uri, String localName, String qName) {

System.out.println("Info:SAXParser endElement chars:'" + charsBuff + "'";

charsBuff.delete(0,charsBuff.length());

}

public void characters(char[] ch, int start, int length) {

charsBuf.append(new String(ch,start,length));

}

hundela at 2007-7-29 16:02:32 > top of Java-index,Java Essentials,Java Programming...
# 4

Several elements if your XML have new lines and you don't say which element is giving you the problem.

P.S. Assuming 'charsBuf' references a StringBuffer or StringBuilder, you don' t need to turn the chars into a String before appending them.

sabre150a at 2007-7-29 16:02:32 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks sabre150. Take a look at the output I posted ...

Info:SAXParser endElement chars:'\n 0'

You can see that the 0 in the input data is preceeded by a newline and several spaces.

hundela at 2007-7-29 16:02:32 > top of Java-index,Java Essentials,Java Programming...
# 6

Try charBuff.setLength(0);

instead ofcharsBuff.delete(0,charsBuff.length());

sabre150a at 2007-7-29 16:02:32 > top of Java-index,Java Essentials,Java Programming...