6 Replies Latest reply: Jul 23, 2007 10:29 AM by 807605 RSS

    Newline From SAX Parser

    807605
      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?
        • 1. Re: Newline From SAX Parser
          807605
          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.
          • 2. Re: Newline From SAX Parser
            807605
            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.
            • 3. Re: Newline From SAX Parser
              807605
              Thanks for your interest. Here is the source, parsing code, and result:

              Info:SAXParser endElement chars:'\n 0'
              <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));
                  }
              • 4. Re: Newline From SAX Parser
                807605
                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.
                • 5. Re: Newline From SAX Parser
                  807605
                  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.
                  • 6. Re: Newline From SAX Parser
                    807605
                    Try
                    charBuff.setLength(0);
                    instead of
                    charsBuff.delete(0,charsBuff.length());