XML – Extensible Markup Language, a human readable, flexible, tree based text format.
The XML document can conform to a scheme, defined with a language named DTD (Document Type Definition).
XML is made of elements. Each element can hold attributes, text and other elements.
Item #1 10 Item #2 2 Item #3 3
There are two common types of XML parsers: SAX parsers and DOM parsers. SAX parsers are event based, the parser raises an event whenever an element is started or closed . DOM parsers build a complete tree which can then be traversed.
Working with XML requires more work compared to JSON. If you can choose between the two, then you should probably choose to use JSON.
The B4X tools include a SAX parser and XML builder libraries. There is also an internal library named Xml2Map, which is based on these two libraries and it behaves similar to the JSON parser and generator.
As XML is more flexible than JSON there are some pitfalls and limitations that you should be aware of when using Xml2Map: https://www.b4x.com/android/forum/threads/b4x-xml2map-simple-way-to-parse-xml-documents.74848/
Generating the above XML:
Dim Items As List = Array( _
CreateMap("Name": "Item #1", "Count": 10), _
CreateMap("Name": "Item #2", "Count": 2), _
CreateMap("Name": "Item #3", "Count": 43))
Dim M2X As Map2Xml
M2X.Initialize
Dim RootAttributes As Map = CreateMap("Version": "1.00", "ID": "abcdef")
Dim xml As String = M2X.MapToXml(CreateMap("root": CreateMap("Attributes": RootAttributes , _
"Items": Items)))
Log(xml)
Parsing it:
Dim X2M As Xml2Map
X2M.Initialize
Dim All As Map = X2M.Parse(xml)
Dim Root As Map = All.Get("root")
Log(Root.Get("Attributes"))
Dim Items as List = Root.Get("Items")
For Each item as Map in Items
Log(item.Get("Name") & ": " & item.Get("Count"))
Next