dimanche 15 mars 2015

Why does SQLite in Android return a String with triple backslash?

SQLite query returns only id=\"reis\"



sqlite> SELECT xml FROM revision r INNER JOIN word w ON r.word_id = w.word_id AND w.word='reis';
<entry id=\"reis\">\n<form>\n<orth>Reis</orth>\n</form>\n<sense><def>_pl._ de [[real:2]].\n</def>\n</sense>\n</entry>


The following test passes in Android but why the <entry id=\\\"reis\\\"> ? Having the triple backslash causes an XmlPullParserException later on



// This test passes.
public void testRevisionXML() {
DictionaryDbHelper dbHelper = new DictionaryDbHelper(mContext);
SQLiteDatabase db = dbHelper.getReadableDatabase();

String word = "reis";
String QUERY = "SELECT r.xml FROM revision r INNER JOIN word w ON r.word_id = w.word_id AND word=?";

Cursor cursor = db.rawQuery(QUERY, new String[]{String.valueOf(word)});

String xmlResult = cursor.getString(0);
String xmlValueExpected = "<entry id=\\\"reis\\\">\\n<form>\\n<orth>Reis</orth>\\n</form>\\n<sense><def>_pl._ de [[real:2]].\\n</def>\\n</sense>\\n</entry>";

assertEquals(xmlValueExpected, xmlResult);
}


The problem is with XmlPullParser


org.xmlpull.v1.XmlPullParserException: attr value delimiter missing! (position:START_TAG @1:11 in java.io.StringReader@14636288)


How can I avoid the parsing exception? Please note that I cannot change the original XML.



// This test crashes with org.xmlpull.v1.XmlPullParserException: attr value delimiter missing!.
public void testParseXMLCrashingTest() throws IOException, XmlPullParserException {

String xmlValueExpected = "<entry id=\\\"reis\\\">\\n<form>\\n<orth>Reis</orth>\\n</form>\\n<sense><def>_pl._ de [[real:2]].\\n</def>\\n</sense>\\n</entry>";

XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(xmlValueExpected));

while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();

Log.d(LOG_TAG, "tag name: " + name);
}
}

// This test passes.
public void testParseXMLPassingTest() throws IOException, XmlPullParserException {

String xmlValueExpected = "<entry id=\"reis\">\\n<form>\\n<orth>Reis</orth>\\n</form>\\n<sense><def>_pl._ de [[real:2]].\\n</def>\\n</sense>\\n</entry>";

XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(xmlValueExpected));

while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();

Log.d(LOG_TAG, "tag name: " + name);
}
}

Aucun commentaire:

Enregistrer un commentaire