我一直在尝试解析 xml 的某些部分,包括 CDATA,但我不明白为什么 cdata 根本没有显示。我一直在尝试用 SWXMLHASH 解决这个问题
下面是xml
<DOC title=\"Effect\" type=\"EE\">
<SECTION title=\"\">
<ARTICLE title=\"1. Main Effect\">
<PARAGRAPH tagName=\"p\" textIndent=\"0\" marginLeft=\"0\">
<![CDATA[Pain due to several diseases]]>
</PARAGRAPH>
</ARTICLE>
<ARTICLE title=\"2. Indications\">
<PARAGRAPH tagName=\"p\" textIndent=\"0\" marginLeft=\"0\">
<![CDATA[Rheuatism]]>
</PARAGRAPH>
</ARTICLE>
</SECTION>
</DOC>
下面是我的代码
import SwiftUI
import SWXMLHash
struct Article : XMLIndexerDeserializable, Identifiable, Hashable {
let id = UUID()
let title : String
let paragraph : [String]
enum CodingKeys: String, CodingKey {
case article = "ARTICLE"
case paragraph = "PARAGRAPH"
}
static func deserialize(_ node: XMLIndexer) throws -> Article {
return try Article (
title: node.value(ofAttribute: "title"),
paragraph: node["ARTICLE"]["PARAGRAPH"].value()
)
}
}
struct MyDrug: View {
@State var drugNameSearch = [DrugNameSearch]()
var body: some View {
GeometryReader{geometry in
ForEach(drugNameSearch, id: \.self){item in
VStack(alignment: .leading, spacing: geometry.size.height/50){
let xmlEffect = item.effect //this is xml same as above
let xml = XMLHash.config { config in
config.shouldProcessLazily = true
}.parse(xmlEffect)
let effectsArticle: [Article] = try! xml["DOC"]["SECTION"]["ARTICLE"].value()
ForEach(effectsArticle, id: \.self){
Text($0.title)
.foregroundColor(Color("tabBarBackground"))
.font(.body)
ForEach($0.paragraph, id: \.self){i in
Text(i)
.foregroundColor(Color("tabBarBackground"))
.font(.body)
}
}
}
}
}
}
}
使用该代码,文章标题正确,但没有段落数据,也没有任何错误。请给我一些建议。。