过去,我创建了一个很好的类来加载和处理 XML 内容。
从资源文件夹加载的用法:
val rootTag=XmlTag.getXmlRootTagOfXmlFileResourceId(context,xmlResId)
或者,如果您在 String 实例中有它:
val rootTag=XmlTag.getXmlFromString(xmlString)
这是代码:
import android.content.Context
import androidx.annotation.RawRes
import androidx.annotation.XmlRes
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.io.StringReader
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
/**
* an xml tag , includes its name, value and attributes
* @param tagName the name of the xml tag . for example : <a>b</a> . the name of the tag is "a"
*/
class XmlTag(val tagName: String) {
/** a hashmap of all of the tag attributes. example: <a c="d" e="f">b</a> . attributes: {{"c"="d"},{"e"="f"}} */
@JvmField
var tagAttributes: HashMap<String, String>? = null
/**list of inner text and xml tags*/
@JvmField
var innerTagsAndContent: ArrayList<Any>? = null
companion object {
@JvmStatic
fun getXmlFromString(input: String): XmlTag? {
val factory = XmlPullParserFactory.newInstance()
factory.isNamespaceAware = true
val xpp = factory.newPullParser()
xpp.setInput(StringReader(input))
return getXmlRootTagOfXmlPullParser(xpp)
}
@JvmStatic
fun getXmlRootTagOfXmlPullParser(xmlParser: XmlPullParser): XmlTag? {
var currentTag: XmlTag? = null
var rootTag: XmlTag? = null
val tagsStack = Stack<XmlTag>()
xmlParser.next()
var eventType = xmlParser.eventType
var doneParsing = false
while (eventType != XmlPullParser.END_DOCUMENT && !doneParsing) {
when (eventType) {
XmlPullParser.START_DOCUMENT -> {
}
XmlPullParser.START_TAG -> {
val xmlTagName = xmlParser.name
currentTag = XmlTag(xmlTagName)
if (tagsStack.isEmpty())
rootTag = currentTag
tagsStack.push(currentTag)
val numberOfAttributes = xmlParser.attributeCount
if (numberOfAttributes > 0) {
val attributes = HashMap<String, String>(numberOfAttributes)
val namespacesCount = xmlParser.getNamespaceCount(xmlParser.depth)
val nameSpaceToNameSpacePrefixMap = HashMap<String, String>(namespacesCount)
for (i in 0 until namespacesCount) {
val prefix: String = xmlParser.getNamespacePrefix(i)
val ns: String = xmlParser.getNamespaceUri(i)
nameSpaceToNameSpacePrefixMap[ns] = prefix
}
for (i in 0 until numberOfAttributes) {
val attrName = xmlParser.getAttributeName(i)
val attrValue = xmlParser.getAttributeValue(i)
xmlParser.getNamespaceCount(1)
val attributeNamespace: String? = xmlParser.getAttributeNamespace(i)
if (attributeNamespace.isNullOrEmpty())
attributes[attrName] = attrValue
else {
val prefix = nameSpaceToNameSpacePrefixMap[attributeNamespace]
?: attributeNamespace
attributes["$prefix:$attrName"] = attrValue
}
}
currentTag.tagAttributes = attributes
}
}
XmlPullParser.END_TAG -> {
currentTag = tagsStack.pop()
if (!tagsStack.isEmpty()) {
val parentTag = tagsStack.peek()
parentTag.addInnerXmlTag(currentTag)
currentTag = parentTag
} else
doneParsing = true
}
XmlPullParser.TEXT -> {
val innerText = xmlParser.text
currentTag?.addInnerText(innerText)
}
}
eventType = xmlParser.next()
}
return rootTag
}
/**returns the root xml tag of the given xml resourceId , or null if not succeeded . */
fun getXmlRootTagOfXmlFileResourceId(context: Context, @XmlRes xmlFileResourceId: Int): XmlTag? {
val res = context.resources
val xmlParser = res.getXml(xmlFileResourceId)
return getXmlRootTagOfXmlPullParser(xmlParser)
}
fun getXmlRootTagFromRawResourceId(context: Context, @RawRes xmlFileResourceId: Int): XmlTag? {
context.resources.openRawResource(xmlFileResourceId).use {
val factory = XmlPullParserFactory.newInstance()
factory.isNamespaceAware = true
val xpp = factory.newPullParser()
xpp.setInput(it, "utf8")
return getXmlRootTagOfXmlPullParser(xpp)
}
}
}
fun addInnerXmlTag(tag: XmlTag) {
if (innerTagsAndContent == null)
innerTagsAndContent = ArrayList()
innerTagsAndContent!!.add(tag)
}
fun addInnerText(str: String) {
if (innerTagsAndContent == null)
innerTagsAndContent = ArrayList()
innerTagsAndContent!!.add(str)
}
/**formats the xmlTag back to its string format,including its inner tags */
override fun toString(): String {
val sb = StringBuilder()
sb.append("<").append(tagName)
val numberOfAttributes = if (tagAttributes != null) tagAttributes!!.size else 0
if (numberOfAttributes != 0)
for ((key, value) in tagAttributes!!)
sb.append(" ").append(key).append("=\"").append(value).append("\"")
val numberOfInnerContent =
if (innerTagsAndContent != null) innerTagsAndContent!!.size else 0
if (numberOfInnerContent == 0)
sb.append("/>")
else {
sb.append(">")
for (innerItem in innerTagsAndContent!!)
sb.append(innerItem.toString())
sb.append("</").append(tagName).append(">")
}
return sb.toString()
}
}