simpleXMLParser is a library written in Kotlin lang that can be used to convert XML into Java/Kotlin Objects representation. It can also be used to convert an XML string to an equivalent Java/Kotlin object.
The library requires to mark data class and each field which should be parsed with annotation @XMLName or @XMLAsArray.
- Provide simple parse() methods to convert input stream with XML or string into Java/Kotlin objects;
- Provide a mechanism to parse elements with the same name into one array (could be usefull for parsing RSS feeds, to be exact 'item' elements)
- Allow custom representations for objects;
- Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types);
- Simple installation by downloading via gradle;
The main class SimpleXmlParser.kt. It contains 2 methods in Companion object:
fun <T> parse(input: InputStream?, clazz: Class<T>): T?
fun <T> parse(input: String?, clazz: Class<T>): T?
Kotlin code:
var entry = SimpleXmlParser.parse(input, TestEntry::class.java!!)
Java code:
TestEntry entry = SimpleXmlParser.Companion.parse(input, TestEntry.class);
Kotlin code:
Can be used with data class
(in current version constructor params should have default value. The library needs default constructor in class)
@XmlName(name = "item")
data class TestEntry(@XmlName(name = "title") var title: String = "",
@XmlName(names = *arrayOf("content:encoded", "description")) var description: String = "")
Also can be used with regular classes:
@XmlName(name = "item")
class TestEntry {
@XmlName(name = "title") var title: String? = null
@XmlName(names = *arrayOf("content:encoded", "description")) var description: String? = null
}
Java code:
@XmlName(name = "channel")
public class TestEntry {
@XmlName(name = "title")
public String title;
@XmlAsArray(name = "item")
public ArrayList<FeedEntryWithAnnotation> items = new ArrayList<>();
}
Please use the SimpleXMLParser issue tracker to discuss anything related to library.
SimpleXMLParser is released under the Apache 2.0 license.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.