ktxml

Minimal non-validating XML pull parser using CharIterator for input, reducing null returns and removing factories. Ideal for lightweight XML parsing in diverse environments.

Android
JVM
iOS
Android Native
macOS
watchOS
tvOS
Linux
Windows
Wasm
JS
GitHub stars37
Authorskobjects
Dependents1
LicenseApache License 2.0
Creation datealmost 5 years ago

Last activityover 1 year ago
Latest release1.0.0 (over 1 year ago)

KtXml

Maven Central

Minimal platform-agnostic non-validating XML pull parser based on kxml2.

The most significant changes (relative to XmlPull / KXml2) are:

  • Replaced the Reader as input with CharIterator support, as Java streams are not available for Kotlin native.
  • Reduced the number of places where null is returned
  • Factories removed

Import in build.gradle.kts for a "shared" KMP module:

(...)

kotlin {
  
      (...)
  
      sourceSets {
          val commonMain by getting {
              dependencies {
                  implementation("org.kobjects.ktxml:core:0.3.2")
                  (...)
              }
          }
          
      (...)  
              

KtXML is implemented in pure Kotlin, so it should work on all platforms supported by Kotlin.

News

  • 2025-06-15 Version bumped to 1.0 and artifacts for all platforms are now included in the Maven artifacts -- thanks to Sargun!

Usage

The most important calls in KtXml are next() and nextToken().

  • next() moves to the next "high level" token and returns the token type. It skips over comments and processing instructions and automatically decodes entity references. It also might aggregate text content.

  • nextToken() moves to the next "low level" token including whitespace, processing instructions, comments and entity references.

After a call to next() or nextToken(), the details of the current token (such as tag attributes) can be queried from the parser; see XmlPullParser

Typically these calls are used to implement recursive descend parsing of a specific XML format.

Please always use the interface (XmlPullParser) and never the concrete parser implementation (MiniXmlPullParser) when passing the parser around.

For a set of small code examples, please take a look at the test.

For more information about the API, please refer to the KtXml API documentation.

Use cases

FAQ

How can I stream data to the parser?

See https://github.com/kobjects/ktxml/issues/6

Where is Wbxml / kDom support from kXML2?

If you are missing anything from kXML2, please file a feature request by creating a corresponding issue in the github issue tracker.

I have a Pull Request for an Improvement

Please file an issue before creating a PR.

Please try to keep PRs as "atomic" as possible, i.e. addressing one issue only. I can push library / Kotlin / Gradle versions up as needed beforehand where this helps.

I have a different problem!

Please file an issue using the github issue tracker.

Background / Design

Brainstorming document: https://docs.google.com/document/d/1OXG5F5I-Gp-65cN8THWB1LMTZDnRS96CIBenPcusDDA/edit?usp=sharing

Android
JVM
iOS
Android Native
macOS
watchOS
tvOS
Linux
Windows
Wasm
JS
GitHub stars37
Authorskobjects
Dependents1
LicenseApache License 2.0
Creation datealmost 5 years ago

Last activityover 1 year ago
Latest release1.0.0 (over 1 year ago)

KtXml

Maven Central

Minimal platform-agnostic non-validating XML pull parser based on kxml2.

The most significant changes (relative to XmlPull / KXml2) are:

  • Replaced the Reader as input with CharIterator support, as Java streams are not available for Kotlin native.
  • Reduced the number of places where null is returned
  • Factories removed

Import in build.gradle.kts for a "shared" KMP module:

(...)

kotlin {
  
      (...)
  
      sourceSets {
          val commonMain by getting {
              dependencies {
                  implementation("org.kobjects.ktxml:core:0.3.2")
                  (...)
              }
          }
          
      (...)  
              

KtXML is implemented in pure Kotlin, so it should work on all platforms supported by Kotlin.

News

  • 2025-06-15 Version bumped to 1.0 and artifacts for all platforms are now included in the Maven artifacts -- thanks to Sargun!

Usage

The most important calls in KtXml are next() and nextToken().

  • next() moves to the next "high level" token and returns the token type. It skips over comments and processing instructions and automatically decodes entity references. It also might aggregate text content.

  • nextToken() moves to the next "low level" token including whitespace, processing instructions, comments and entity references.

After a call to next() or nextToken(), the details of the current token (such as tag attributes) can be queried from the parser; see XmlPullParser

Typically these calls are used to implement recursive descend parsing of a specific XML format.

Please always use the interface (XmlPullParser) and never the concrete parser implementation (MiniXmlPullParser) when passing the parser around.

For a set of small code examples, please take a look at the test.

For more information about the API, please refer to the KtXml API documentation.

Use cases

FAQ

How can I stream data to the parser?

See https://github.com/kobjects/ktxml/issues/6

Where is Wbxml / kDom support from kXML2?

If you are missing anything from kXML2, please file a feature request by creating a corresponding issue in the github issue tracker.

I have a Pull Request for an Improvement

Please file an issue before creating a PR.

Please try to keep PRs as "atomic" as possible, i.e. addressing one issue only. I can push library / Kotlin / Gradle versions up as needed beforehand where this helps.

I have a different problem!

Please file an issue using the github issue tracker.

Background / Design

Brainstorming document: https://docs.google.com/document/d/1OXG5F5I-Gp-65cN8THWB1LMTZDnRS96CIBenPcusDDA/edit?usp=sharing