In my Android application, the user needs to scan the Indian Aadhar QR code. After reading the QR code it returns following response. I can not parse it. Please help me fix this
String xml = "<?xml version="1.0" encoding="UTF-8"?>
<PrintLetterBarcodeData uid="00X575452391"
name="XX"
gender="M"
yob="1992"
gname="XX"
co="S/O: XX"
house="4-175"
street="XX"
lm="XX"
vtc="XX"
po="XX"
dist="XX"
subdist="XX"
state=""
pc="XX"
dob="XX/XX/XXXX"/>"
Edit 1: Added what I tried for XML parsing.
I have tried this solution. But, it doesn't help me
try{
JSONObject xmlJSONObj = XML.toJSONObject(xml);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
Edit 2: I have fixed it by myself using this code
public String xmlParser(String xml) {
try{
JSONObject xmlJSONObj = XML.toJSONObject(xml);
return xmlJSONObj.toString(0);
} catch (JSONException je) {
return null;
}
}
To use XML.toJSONObject, you will first need to download the java-json lib and include it in your project. You can use this link to find a downloadable jar:
http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm
Place downloaded the file in app/libs folder and add the dependency
implementation fileTree(dir: 'libs', include: ['*.jar'])
Then you can use the code