我目前有以下 XML
<monster name="Valkyrie" nameDescription="a valkyrie" race="blood" experience="85" speed="190" manacost="450">
<health now="190" max="190" />
<look type="139" head="113" body="57" legs="95" feet="113" corpse="20523" />
<voices interval="5000" chance="10">
<voice sentence="Another head for me!" />
<voice sentence="Head off!" />
<voice sentence="Your head will be mine!" />
<voice sentence="Stand still!" />
<voice sentence="One more head for me!" />
</voices>
</monster>
我正在使用以下结构阅读它
type monster struct {
XMLName xml.Name `xml:"monster"`
Name string `xml:"name,attr"`
NameDescription string `xml:"nameDescription,attr"`
Race string `xml:"race,attr"`
Experience int `xml:"experience,attr"`
Speed int `xml:"speed,attr"`
ManaCost int `xml:"manacost,attr"`
Health monsterHealth `xml:"health"`
Look monsterLook `xml:"look"`
Voices monsterVoice `xml:"voices"`
}
type monsterVoice struct {
Voices []monsterSentence
}
type monsterSentence struct {
XMLName xml.Name `xml:"voice"`
Sentence string `xml:"sentence,attr"`
}
type monsterLook struct {
Type int `xml:"type,attr"`
Head int `xml:"head,attr"`
Body int `xml:"body,attr"`
Legs int `xml:"legs,attr"`
Feet int `xml:"feet,attr"`
Corpse int `xml:"corpse,attr"`
}
type monsterHealth struct {
Now int `xml:"now,attr"`
Max int `xml:"max,attr"`
}
但我不确定如何读取语音元素
最佳答案
您只是错过了为 Voices 指定 XML 元素映射:
type monsterVoice struct {
Voices []monsterSentence `xml:"voice"`
}
在那个小的添加之后,像往常一样解码应该可以工作:
var result monster
err := xml.Unmarshal([]byte(your_xml_data_string), &result)
if err != nil {
fmt.Println(err)
}
for _, r := range result.Voices.Voices {
fmt.Println(r.Sentence)
}
更好的是,删除 monsterVoice 并像这样使用子选择器:
type monster struct {
XMLName xml.Name `xml:"monster"`
....
Voices []monsterSentence `xml:"voices>voice"`
}
然后我们就可以去掉之前demo中那个笨拙的result.Voices.Voices了:
for _, r := range result.Voices {
fmt.Println(r.Sentence)
}
输出:(两个演示产生相同的输出)
Another head for me!
Head off!
Your head will be mine!
Stand still!
One more head for me!
关于xml - 解码 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37629947/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::
我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi
这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::
我正在尝试加载SAML协议(protocol)架构(具体来说:https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd),但在执行此操作之后:schema=Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))我得到这个输出:Nokogiri::XML::SyntaxErrorException:Element'{http://www.w3.org/2001/XMLSchema}element',att
我有一个使用postgresql的Rails4应用程序。我还有一个backbone.js应用程序,可将JSON推送到Rails4应用程序。这是我的Controller:defcreate@product=Product.new(ActiveSupport::JSON.decodeproduct_params)respond_todo|format|if@product.saveformat.json{renderaction:'show',status::created,location:@product}elseformat.json{renderjson:@product.erro
我正在尝试通过POST将XML内容发送到一个简单的Rails项目中的Controller(“解析”)方法(“索引”)。它不是RESTful,因为我的模型名称不同,比如“汽车”。我在有效的功能测试中有以下内容:deftest_index...data_file_path=File.dirname(__FILE__)+'/../../app/views/layouts/index.xml.erb'message=ERB.new(File.read(data_file_path))xml_result=message.result(binding)doc=REXML::Document.ne
我正在尝试序列化和反序列化哈希。当散列被反序列化时,键被去符号化;例如不是更多:一个,而是“一个”。从Rails控制台:>>h={:one=>1,:two=>"two"}{:one=>1,:two=>"two"}>>j=ActiveSupport::JSON.encode(h)"{\"one\":1,\"two\":\"two\"}">>h2=ActiveSupport::JSON.decode(j){"one"=>1,"two"=>"two"}>>h2[:one]nil>>h[:one]1我现在已经切换到使用Marshal.dump/load。但是,我想把它扔出去看看是否有办法将它保
我有这样的代码:@doc=Nokogiri::HTML(open(url)@doc.xpath(query).eachdo|html|putshtml#howgetcontentofanodeend我如何获取节点的内容而不是像这样: 最佳答案 这是READMEfile中的概要示例为Nokogiri展示了一种使用CSS、XPath或混合的方法:require'nokogiri'require'open-uri'#GetaNokogiri::HTML:Documentforthepagewe’reinterestedin...doc=N
恐怕我没有太多通过网络服务器发布文档(例如XML)的经验,所以如果我对HTTP的理解不足,我深表歉意。我在127.0.0.1上的ruby应用程序中设置了一个基本的MongrelWeb服务器端口2000.(服务器)。我在同一台计算机上运行一个单独的Ruby应用程序。(客户)。我需要客户端向服务器发送XML文档。我曾尝试使用Net::HTTP来执行此操作,但我找不到一个明确的示例来告诉我应该做什么。我试过了,但遇到了错误。我已将请求分解,使其尽可能基本:http=Net::HTTP.new("127.0.0.1",2000)http.post('file','query=foo')#x