0

我一直在尝试解析 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)
               }
                                                    
                                                        
         }                                          
                                                
                                                
                                                
      }                                        
     }
    }
   }
  }

使用该代码,文章标题正确,但没有段落数据,也没有任何错误。请给我一些建议。。

4

1 回答 1

0

主要问题是,在您的自定义Article类型中,当检索 PARAGRAPH 的值时,它应该是:paragraph: node["PARAGRAPH"].value()

这是因为该节点已被 ARTICLE 索引,因此您不必指定该索引。所以,整个文章类型看起来像:

    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["PARAGRAPH"].value()
            )
        }
    }

要检查的另一件事......如果不会超过一个段落,那么可以将其定义为String而不是[String].

于 2022-01-08T22:15:34.530 回答