0

我想用 SWXMLHash 反序列化一组标签的序列。

这是一个示例 XML:

<?xml version="1.0"?>
<Places>
   <Place> 
      <name>First Place</name>
      <lon>6.775907</lon>
      <lat>51.429786</lat>
      <lon>6.775907</lon>
      <lon>6.775907</lon>
      <lat>51.429786</lat>
    </Place>
    <Place> 
       <name>Second Place</name> 
       <lon>10.885568</lon>
    </Place> 
    <Place> 
       <name>Third Place</name> 
       <lon>6.775907</lon> 
       <lat>51.429786</lat> 
       </Place>
    <Place>
       <name>Fourth Place</name> 
       <lon>10.885568</lon> 
       <lon>11.885568</lon> 
    </Place> 
</Places>

我尝试了以下(示例)代码:

struct Location: XMLIndexerDeserializable {
    let lon : Double
    let lat : Double?
    
    static func deserialize(_ node: XMLIndexer) throws -> Location {
        return try Location(
            lat: node["lat"].value(),
            lon: node["lon"].value()
        )
    }
}

struct Place: XMLIndexerDeserializable {
    let name : String
    let locations : [Location]?
    
    static func deserialize(_ node: XMLIndexer) throws -> Place {
        return try Place(
            name: node["name"].value(),
            locations: node.value()
        )
    }
}

收到该节点的错误消息。无效

4

0 回答 0