The following code will convert Json to Xml and Xml to Json
Required library's xmbuilder,json,javaobject
Requires B4A 3.80 or B4j 2.20
The attached library source is https://github.com/douglascrockford/JSON-java
the attached version excludes JavaException.java as it is already part of B4A
Copy the json-java.jar to the additional library folder
TAGS Json to Xml, Xml to Json
Required library's xmbuilder,json,javaobject
Requires B4A 3.80 or B4j 2.20
The attached library source is https://github.com/douglascrockford/JSON-java
the attached version excludes JavaException.java as it is already part of B4A
Copy the json-java.jar to the additional library folder
B4X:
#Region Project Attributes
#ApplicationLabel: JsXl
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#AdditionalJar: Java-Json
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
'Reference http://www.json.org/java/
'The original library contained JSONException.java and was removed as it is included already
'the rest of the java-json functions are available
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
If FirstTime Then
'open the stored xml file for reading
'your can use any xml string and convert it to json
Dim TextReader1 As TextReader
TextReader1.Initialize(File.OpenInput(File.DirAssets, "Books.xml"))
Dim inputxml As String
inputxml = TextReader1.ReadAll
'Convert the inputxml to json
Log("Converted json string")
Log(xmltojson(inputxml))
'Lets just get the converted json string and convert it back again
Dim jsonstring As String
jsonstring = xmltojson(inputxml)
Log("Converted json to xml string")
'jsontoxml now expects a boolean value to include namespace
Log(jsontoxml(jsonstring,False))
End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub jsontoxml(jsontext As String, includeNamespace As Boolean) As String
Dim x As XMLBuilder
Dim jo As JavaObject
jo.InitializeNewInstance("org.json.JSONObject",Array As Object(jsontext))
Dim jo2 As JavaObject
jo2.InitializeNewInstance("org.json.XML", Null)
Dim xml As String = jo2.RunMethod("toString", Array(jo))
'if you need to include an xmlnamespace set includenamespace to true
If includeNamespace Then
x = x.Create("top") _
.text(xml) _
.up()
Dim xmlProps As Map
xmlProps.Initialize
xmlProps.Put("indent", "yes")
Return x.asString2(xmlProps).Replace("<","<").Replace(">",">")
Else
Return xml
End If
End Sub
Sub xmltojson(xml As String) As String
'nothing to do in this sub it just works
Dim jo As JavaObject
Dim JSON As JSONParser
Dim jg1 As JSONGenerator
jo.InitializeNewInstance("org.json.XML", Null)
Dim jml As String = jo.RunMethod("toJSONObject", Array(xml))
Dim Map1 As Map
JSON.Initialize(jml)
Map1 = JSON.NextObject
jg1.Initialize(Map1)
Return jg1.ToPrettyString(4)
End Sub
TAGS Json to Xml, Xml to Json
Attachments
Last edited: