Preserve newlines when parsing xml

2020-04-17 06:04发布

问题:

I'm using the SAX xml parser to parse some xml data which contains newlines. When using Attributes#getValue, the newline data is lost. How can keep the newlines?

回答1:

you can use this code when getting the String to parse:

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


      for(int i=start; i<length; i++)
      if(!Character.isISOControl(ch[i]))
          content.append(ch[i]);

  } 


回答2:

The solution was to use &#xA; instead of \n