我需要使用Nokogiri Gem从CodigoImovel == 6124-2
获取以下XML的值。
<?xml version="1.0" encoding="UTF-8"?>
<Carga xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Imoveis>
<Imovel>
<CodigoImovel>6124-2</CodigoImovel>
<TipoImovel>Apartamento</TipoImovel>
<SubTipoImovel>Apartamento Padrão</SubTipoImovel>
</Imovel>
<Imovel>
<CodigoImovel>86765</CodigoImovel>
<TipoImovel>Apartamento</TipoImovel>
<SubTipoImovel>Apartamento Padrão</SubTipoImovel>
<CategoriaImovel>Cobertura</CategoriaImovel>
</Imovel>
<Imovel>
<CodigoImovel>981768</CodigoImovel>
<TipoImovel>Casa</TipoImovel>
<SubTipoImovel>Casa de Condomínio</SubTipoImovel>
<CategoriaImovel>Térrea</CategoriaImovel>
</Imovel>
<Imovel>
<CodigoImovel>357468</CodigoImovel>
<TipoImovel>Casa</TipoImovel>
<SubTipoImovel>Casa de Condomínio</SubTipoImovel>
<CategoriaImovel>Térrea</CategoriaImovel>
</Imovel>
<Imovel>
<CodigoImovel>587168</CodigoImovel>
<TipoImovel>Comercial/Industrial</TipoImovel>
<SubTipoImovel>Conjunto</SubTipoImovel>
<CategoriaImovel>Comercial/Sala Padrão</CategoriaImovel>
</Imovel>
</Imoveis>
</Carga>
我用它来获取价值 xml.xpath(&#34; // // Carga // Imoveis Imovel [CodigoImovel =&#39;&#34 + + re_code&#34;&#39;]&#34) 但它表示未定义的局部变量或方法`re_code&#39;为# 我感谢任何帮助。谢谢!
答案 0 :(得分:0)
我做了以下操作,可以得到你需要的节点:
require 'nokogiri'
xml = Nokogiri::XML::Document.parse '<Carga
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Imoveis>
<Imovel>
<CodigoImovel>6124-2</CodigoImovel>
<TipoImovel>Apartamento</TipoImovel>
<SubTipoImovel>Apartamento Padrão</SubTipoImovel>
</Imovel>
<Imovel>
<CodigoImovel>86765</CodigoImovel>
<TipoImovel>Apartamento</TipoImovel>
<SubTipoImovel>Apartamento Padrão</SubTipoImovel>
<CategoriaImovel>Cobertura</CategoriaImovel>
</Imovel>
<Imovel>
<CodigoImovel>981768</CodigoImovel>
<TipoImovel>Casa</TipoImovel>
<SubTipoImovel>Casa de Condomínio</SubTipoImovel>
<CategoriaImovel>Térrea</CategoriaImovel>
</Imovel>
<Imovel>
<CodigoImovel>357468</CodigoImovel>
<TipoImovel>Casa</TipoImovel>
<SubTipoImovel>Casa de Condomínio</SubTipoImovel>
<CategoriaImovel>Térrea</CategoriaImovel>
</Imovel>
<Imovel>
<CodigoImovel>587168</CodigoImovel>
<TipoImovel>Comercial/Industrial</TipoImovel>
<SubTipoImovel>Conjunto</SubTipoImovel>
<CategoriaImovel>Comercial/Sala Padrão</CategoriaImovel>
</Imovel>
</Imoveis>
</Carga>'
在解析之后和最后的单引号。然后我做了这个
re_code = '6124-2'
xml.xpath("//Carga//Imoveis//Imovel[CodigoImovel='"+re_code+"']")
获得了第一个带有子节点的<Imovel>
节点。虽然我不确定你为什么叫它re_code
,你的意思是在这里使用正则表达式吗?