• Skip to main content
  • Skip to footer

B4X

Develop Android, iOS and IoT applications

  • Home
  • Products
    • B4A (Android)
    • B4i (iOS)
    • B4J (Desktop)
    • B4R (Arduino)
  • Showcase
  • Store
  • Learn
    • General
    • Guides
    • Video Tutorials
    • Glossary
  • Teach
  • Blog
  • Community
A B C D F G H J M N Q S T U X

All

XML

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.


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root ID="abcdef" Version="1.00">
    <Items>
        <Item>
            <Name>Item #1</Name>
            <Count>10</Count>
        </Item>
        <Item>
            <Name>Item #2</Name>
            <Count>2</Count>
        </Item>
        <Item>
            <Name>Item #3</Name>
            <Count>3</Count>
        </Item>
    </Items>
</root>

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

Written by Erel Uziel

Footer

Top

  • Home
  • Products
    • B4A (Android)
    • B4i (iOS)
    • B4J (Desktop)
    • B4R (Arduino)
  • Showcase
  • Store
  • Learn
    • General
    • Guides
    • Video Tutorials
    • Glossary
  • Teach
  • Blog
  • Community

About us

Follow us:

Latest Versions

B4A v10.5 (changelog)
B4i v6.80 (changelog)
B4J v8.80 (changelog)
B4R v3.50 (changelog)

Contact Us

support@basic4ppc.com

Privacy Policy

Privacy Policy

Copyright © 2021 · Anywhere Software. Android is a trademark of Google Inc. iOS is a registered trademark of Apple. Arduino is a trademark of Arduino. Java is a trademark of Oracle.