java中org.xml.sax不能读取xml回车换行的问题解决(android)
android使用sax解析xml时,碰到换行符不能读取的问题,以下方法可以解决,定义temp变量累加new String(ch,start,length)读取的值,最后在endElement方法中赋值给你需要的属性或变量就可以了,原因可能是sax循环读取字符内容时,碰到回车就会扔掉之前读取的值。
1 private String temp = "";
2
3 /**
4 * Gets be called on the following structure: <tag>characters</tag>
5 */
6 @Override
7 public void characters(char ch[], int start, int length) {
8 if (this.in_mytag) {
9 String strTag = new String(ch, start, length);
10 temp = temp + strTag;
11 // myParsedXmlDataSet.setExtractedString(new String(ch, start,
12 // length));
13 }
14 }
15
16 @Override
17 public void endElement(String namespaceURI, String localName, String qName)
18 throws SAXException {if (localName.equals("mytag")) {
19 this.in_mytag = false;
20 myParsedXmlDataSet.setExtractedString(temp);
21 temp = "";
22 }
23 }
2
3 /**
4 * Gets be called on the following structure: <tag>characters</tag>
5 */
6 @Override
7 public void characters(char ch[], int start, int length) {
8 if (this.in_mytag) {
9 String strTag = new String(ch, start, length);
10 temp = temp + strTag;
11 // myParsedXmlDataSet.setExtractedString(new String(ch, start,
12 // length));
13 }
14 }
15
16 @Override
17 public void endElement(String namespaceURI, String localName, String qName)
18 throws SAXException {if (localName.equals("mytag")) {
19 this.in_mytag = false;
20 myParsedXmlDataSet.setExtractedString(temp);
21 temp = "";
22 }
23 }