简体   繁体   English

如何解析 kotlin 中的 xml 字符串(不是来自文件)

[英]how to parse xml string in kotlin (not from a file)

I have Kotlin code that others have written that parses an xml file.我有其他人编写的解析 xml 文件的 Kotlin 代码。 The code is here: https://www.javatpoint.com/kotlin-android-xmlpullparser-tutorial .代码在这里: 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.我想更改它以便它解析一个字符串,例如来自 textview 的字符串。 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我在 Java 中有代码可以解析一个字符串,但它不在 Kotlin 中。代码在这里: 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:关键区别似乎在于设置输入 stream。Kotlin 代码片段如下所示:

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:而 java 代码如下所示:

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.所以我想问题归结为 where.setInput 完成了。 That is, does Kotlin have a StringReader?也就是说,Kotlin 是否有 StringReader? or similar function?或类似的 function? I tried to using Android Studio to convert the java code to Kotlin and it looks like still wants to use Java libraries.我尝试使用 Android Studio 将 java 代码转换为 Kotlin,看起来仍然想使用 Java 库。 Is that ok if one wants a true Kotlin app?如果想要一个真正的 Kotlin 应用程序,可以吗? 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()
        }
    }
}

Yes, you can just use a StringReader .是的,您可以只使用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.如果您愿意,您甚至可以使用reader()扩展 function 编写setInput("<foo>Hello World.</foo>".reader())使它更 Kotliny。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM