Home > OS >  how to parse xml string in kotlin (not from a file)
how to parse xml string in kotlin (not from a file)

Time:10-12

I have Kotlin code that others have written that parses an xml file. The code is here: https://www.javatpoint.com/kotlin-android-xmlpullparser-tutorial. I would like to change it so that it parses a string, say from textview for example. I have code in Java that pareses a string, but it's not in Kotlin. That code is here: https://developer.android.com/reference/kotlin/org/xmlpull/v1/XmlPullParser

The key difference seems to be on setting the input stream. The Kotlin code snippet looks like:

fun parse(inputStream: InputStream): List<Employee> {  
        try {  
            val factory = XmlPullParserFactory.newInstance()  
            factory.isNamespaceAware = true  
            val parser = factory.newPullParser()  
            parser.setInput(inputStream, null)  

whereas the java code looks like:

public static void main (String args[])
        ​throws XmlPullParserException, IOException
    ​{
        ​XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        ​factory.setNamespaceAware(true);
        ​XmlPullParser xpp = factory.newPullParser();

        ​xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );

so I guess the question boils to where .setInput is done. That is, does Kotlin have a StringReader? or similar function? I tried to using Android Studio to convert the java code to Kotlin and it looks like still wants to use Java libraries. Is that ok if one wants a true Kotlin app? The conversion is here:

import kotlin.Throws
import org.xmlpull.v1.XmlPullParserException
import kotlin.jvm.JvmStatic
import org.xmlpull.v1.XmlPullParserFactory
import org.xmlpull.v1.XmlPullParser
import java.io.IOException
import java.io.StringReader

object SimpleXmlPullApp {
    @Throws(XmlPullParserException::class, IOException::class)
    @JvmStatic
    fun main(args: Array<String>) {
        val factory = XmlPullParserFactory.newInstance()
        factory.isNamespaceAware = true
        val xpp = factory.newPullParser()
        xpp.setInput(StringReader("<foo>Hello World!</foo>"))
        var eventType = xpp.eventType
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_DOCUMENT) {
                println("Start document")
            } else if (eventType == XmlPullParser.END_DOCUMENT) {
                println("End document")
            } else if (eventType == XmlPullParser.START_TAG) {
                println("Start tag "   xpp.name)
            } else if (eventType == XmlPullParser.END_TAG) {
                println("End tag "   xpp.name)
            } else if (eventType == XmlPullParser.TEXT) {
                println("Text "   xpp.text)
            }
            eventType = xpp.next()
        }
    }
}

CodePudding user response:

Yes, you can just use a StringReader. If you like, you can even make it a little more Kotliny by writing setInput("<foo>Hello World!</foo>".reader()), using the reader() extension function.

  • Related