使用来自 here 的库.这是详细信息,
XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<status code="25">Verification required</status>
<parsed-challenge>
<action type-id="11">
<url content-type="application/x-www-form-urlencoded" method="POST" type-id="1">https://example.com</url>
<hidden-fields>
<apiRequest>MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD</apiRequest>
</hidden-fields>
</action>
</parsed-challenge>
<return-url>https://example.com</return-url>
</root>
结构:
struct Secure: XMLMappable {
internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?
public var nodeName: String!
public init(map: XMLMap) {
statusCode = map.value()
status = map.value()
actionType = map.value()
url = map.value()
hiddenFields = map.value()
returnURL = map.value()
}
public mutating func mapping(map: XMLMap) {
statusCode <- map["status"].attributes["code"]
status <- map["status"].innerText
actionType <- map["parsed-challenge.action"].attributes["type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
解码时,
Secure(statusCode: nil, status: nil, actionType: nil, url: Optional(URLInfo(url: Optional(https://example.com), method: Optional("POST"), contentType: Optional("application/x-www-form-urlencoded"), typeId: Optional(1))), hiddenFields: Optional(["__name": "hidden-fields", "apiRequest": "MIAGCSqGSIb3DQEHA6CAMIACAQAxggFAMIIBPAIBAD"]), returnURL: Optional(https://example.com))
status、statusCode 和 actionType 有什么问题?为什么他们不解码,是因为无法进行深度嵌套解码吗?
最佳答案
我希望我的图书馆能够像那样工作。 (这样嵌套值的映射非常干净)但是目前很难映射嵌套属性和内部具有其他元素或属性的元素的 innerText。
因此,建议的映射 XML 的模型如下所示:
struct Secure: XMLMappable {
public var nodeName: String!
internal(set) var status: Status?
internal(set) var action: Action?
internal(set) var returnURL: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
status <- map["status"]
action <- map["parsed-challenge.action"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
struct Status: XMLMappable {
public var nodeName: String!
internal(set) var statusCode: Int?
internal(set) var message: String?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
statusCode <- map.attributes["code"]
message <- map.innerText
}
}
struct Action: XMLMappable {
public var nodeName: String!
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
actionType <- map.attributes["type-id"]
url <- map["url"]
hiddenFields <- map["hidden-fields"]
}
}
struct URLInfo: XMLMappable {
public var nodeName: String!
internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}
虽然我个人使用(因为我知道如何破解我的图书馆)我可以使用这样的东西:
struct Secure: XMLMappable {
public var nodeName: String!
internal(set) var statusCode: Int?
internal(set) var status: String?
internal(set) var actionType: Int?
internal(set) var url: URLInfo?
internal(set) var hiddenFields: [String:String]?
internal(set) var returnURL: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
statusCode <- map["status._code"]
status <- map["status.__text"]
actionType <- map["parsed-challenge.action._type-id"]
url <- map["parsed-challenge.action.url"]
hiddenFields <- map["parsed-challenge.action.hidden-fields"]
returnURL <- (map["return-url"], XMLURLTransform())
}
}
struct URLInfo: XMLMappable {
public var nodeName: String!
internal(set) var contentType: String?
internal(set) var method: String?
internal(set) var typeID: Int?
internal(set) var url: Foundation.URL?
public init(map: XMLMap) { }
public mutating func mapping(map: XMLMap) {
contentType <- map.attributes["content-type"]
method <- map.attributes["method"]
typeID <- map.attributes["type-id"]
url <- (map.innerText, XMLURLTransform())
}
}
两种模型都可以正常工作。
如果嵌套映射在下一个版本中得到改进,我一定会更新这篇文章。
关于ios - Swift XMLMapper 解码嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163370/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的