XMLCoder 不能直接用 Codable 解析任意 XML,因其仅支持结构清晰、可映射为 Swift 类型层次的 XML,不支持混合内容、PI、DTD、注释、默认命名空间等;需用 XMLText 处理纯文本节点,数组自动聚合同名元素,可选字段和嵌套结构按 Codable 规则处理。
XMLCoder 不能直接用 Codable 解析任意 XML
Swift 原生 Codable 协议只支持 JSON(通过 JSONEncoder/JSONDecoder),不支持 XML。XMLCoder 是第三方库,它**扩展了 Decoder 和 Encoder 协议**,让 Codable 类型能被用于 XML 编解码,但前提是 XML 结构必须能映射为 Swift 的类型层次 —— 它不是通用 XML 解析器(比如不能处理混合内容、PI、DTD 或任意嵌套文本节点)。
- 它要求 XML 有清晰的层级结构,标签名与属性名需对应 Swift 属性名(或通过
@XMLKey显式标注) - 不支持 XML 注释、处理指令、命名空间(除非手动配置
XMLDecoder.KeyEncodingStrategy或自定义KeyedDecodingContainer) - 默认把 XML 属性(
attr="value")映射为 Swift 属性,把子元素映射为嵌套结构;文本内容需显式声明为XMLText类型
基础用法:定义 Codable 类型 + XMLDecoder.decode(_:from:)
先用 Swift 包管理器添加依赖:https://github.com/MaxDesiatov/XMLCoder,然后定义符合 Codable 的结构体。注意:XML 标签名和 Swift 属性名默认按 snake_case ↔ kebab-case 自动转换(如 user_name ↔ user-name),可通过 keyEncodingStrategy 调整。
import XMLCoder
struct User: Codable {
let id: Int
let name: String
let isActive: Bool
enum CodingKeys: String, CodingKey {
case id = "user-id"
case name = "full-name"
case isActive = "active"
}
}
let xml = """
Alice
true
"""
do {
let user = try XMLDecoder().decode(User.self, from: xml.data(using: .utf8)!)
print(user.id) // 123
} catch {
print("Decode failed:", error)
}
解析带文本内容的元素(如 9.99)
如果 XML 元素只有纯文本(无子元素、无属性),必须用 XMLText 类型声明,否则解码会失败 —— 这是新手最常踩的坑。