forked from getodk/collect
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature to get submitted form data as json in OnFormUploaded ev…
…ent.
- Loading branch information
1 parent
268c6e7
commit 819e342
Showing
4 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
odk/collect/collect_app/src/main/java/org/odk/collect/utilities/XmlJsonUtility.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.odk.collect.utilities | ||
|
||
import org.json.JSONArray | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
import org.xmlpull.v1.XmlPullParser | ||
import org.xmlpull.v1.XmlPullParserException | ||
import org.xmlpull.v1.XmlPullParserFactory | ||
import java.io.File | ||
import java.io.IOException | ||
import java.io.StringReader | ||
|
||
object XmlJsonUtility { | ||
|
||
@Throws(XmlPullParserException::class, IOException::class, JSONException::class) | ||
fun convertToJson(xml: String?): JSONObject { | ||
val factory = XmlPullParserFactory.newInstance() | ||
val parser = factory.newPullParser() | ||
parser.setInput(StringReader(xml)) | ||
val jsonObject = JSONObject() | ||
var jsonArray = JSONArray() | ||
var tagName = "" | ||
var eventType = parser.eventType | ||
while (eventType != XmlPullParser.END_DOCUMENT) { | ||
when (eventType) { | ||
XmlPullParser.START_DOCUMENT -> {} | ||
XmlPullParser.START_TAG -> { | ||
tagName = parser.name | ||
if (jsonObject.has(tagName)) { | ||
// If the tag already exists in the object, convert it to an array | ||
val existingValue = jsonObject[tagName] | ||
if (existingValue is JSONArray) { | ||
jsonArray = existingValue | ||
} else { | ||
jsonArray = JSONArray() | ||
jsonArray.put(existingValue) | ||
jsonObject.put(tagName, jsonArray) | ||
} | ||
} | ||
} | ||
XmlPullParser.TEXT -> if (tagName.isNotEmpty()) { | ||
jsonArray.put(parser.text) | ||
} | ||
XmlPullParser.END_TAG -> { | ||
if (tagName.isNotEmpty()) { | ||
if (jsonArray.length() > 0) { | ||
jsonObject.put(tagName, jsonArray) | ||
jsonArray = JSONArray() | ||
} else { | ||
jsonObject.put(tagName, "") | ||
} | ||
} | ||
tagName = "" | ||
} | ||
} | ||
eventType = parser.next() | ||
} | ||
return jsonObject | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun convertToJson(xmlFile: File): JSONObject { | ||
val xmlString = xmlFile.readText() | ||
return convertToJson(xmlString) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters