简体   繁体   English

Swift, xml 用 SWXMLHASH 解析

[英]Swift, xml parsing with SWXMLHASH

I 've been trying to parse some part of xml including CDATA and I don't understand why the cdata was not showing at all.我一直在尝试解析 xml 的某些部分,包括 CDATA,但我不明白为什么 cdata 根本没有显示。 I ve been trying to solve this problem with SWXMLHASH我一直在尝试用 SWXMLHASH 解决这个问题

Below is the xml下面是 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>

And below is my code下面是我的代码

    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)
               }
                                                    
                                                        
         }                                          
                                                
                                                
                                                
      }                                        
     }
    }
   }
  }

With that code, title of Article comes right, but no Paragraph data comes and no error at all.使用该代码,文章标题正确,但没有段落数据,也没有任何错误。 Please give me some advice..请给我一些建议。。

The main issue is that, in your custom Article type, when the value of PARAGRAPH is retrieved, it should instead be like: paragraph: node["PARAGRAPH"].value()主要问题是,在您的自定义Article类型中,当检索 PARAGRAPH 的值时,它应该类似于: paragraph: node["PARAGRAPH"].value()

This is because the node is already indexed to the ARTICLE so you don't have to specify that index.这是因为该节点已被 ARTICLE 索引,因此您不必指定该索引。 So, the whole Article type would look like:所以,整个文章类型看起来像:

    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()
            )
        }
    }

One other thing to check... if there won't be more than one paragraph, then that can be defined as String instead of [String] .要检查的另一件事......如果不会超过一个段落,那么可以将其定义为String而不是[String]

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

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