xml - groovy XmlSlurper 不解析我的 xml 文件

标签 xml grails groovy xmlslurper

我有一个 xml,但我无法用 xmlslurper 解析这个文件。 这是我的 xml 文件的副本:

<Entrezgene-Set>
<Entrezgene>
<Entrezgene_summary>The protein encoded by this gene is a plasma glycoprotein of unknown function. The protein shows sequence similarity to the variable regions of some immunoglobulin supergene family member proteins. [provided by RefSeq]</Entrezgene_summary>
</Entrezgene>
</Entrezgene-Set>

我只需要从 <Entrezgene_summary> 获取文本

这里是我的代码:

  def pubmedEfetch = {

  def base = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?"
  def qs = []
  qs << "db=gene"
  qs << "id=1"
  qs << "retmode=xml"
  def url = new URL(base + qs.join("&"))
  def connection = url.openConnection()

  def result = [:]

  if(connection.responseCode == 200){
    def xml = connection.content.text
    def eFetchResult = new XmlSlurper().parseText(xml)
    result.geneSummary = eFetchResult.Entrezgene-Set.Entrezgene.Entrezgene_summary
  }
  else{
    log.error("PubmedEfetchParserService.PubmedEsearch FAILED")
    log.error(url)
    log.error(connection.responseCode)
    log.error(connection.responseMessage)
  }
  render result
}

还有我的错误信息:

Error 500: groovy.lang.MissingPropertyException: No such property: Entrezgene for class: java.util.Set
Servlet: grails
URI: /geneInfo/grails/genes/pubmedEfetch.dispatch
Exception Message: No such property: Entrezgene for class: java.util.Set 
Caused by: groovy.lang.MissingPropertyException: No such property: Entrezgene for class: java.util.Set 
Class: GenesController 

我不明白我的错在哪里?

我也尝试:result.geneSummary = eFetchResult./Entrezgene-Set/.Entrezgene.Entrezgene_summary

有人有想法吗? 谢谢

最佳答案

您不需要取消引用顶部标签 (Entersgene-Set>)。以下在 groovyconsole 中对我有用:

xml = """<Entrezgene-Set>
<Entrezgene>
   <Entrezgene_summary>The protein encoded by this gene is a plasma glycoprotein of unknown function. The protein shows sequence similarity to the variable regions of some immunoglobulin supergene family member proteins. [provided by RefSeq]
   </Entrezgene_summary>
</Entrezgene>
</Entrezgene-Set>
"""


def eFetchResult = new XmlSlurper().parseText(xml)
x = eFetchResult.Entrezgene.Entrezgene_summary
println "x is [${x}]"

顺便说一句,您的错误消息是由于尝试使用其中包含破折号的属性名称引起的。

关于xml - groovy XmlSlurper 不解析我的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1849547/

相关文章:

xml - 获取 Notepad++ XML 解析错误 "Extra content at the end of the document",即使没有

grails - Grails 3域类组成

java - 寻找便利工厂来创建 GroovyObjectSupport 实例

python - 用python解析nmap xml报告

java - 解析此 XML 时我做错了什么

java - 用于多个包的 MOXy XmlNameTransformer

javascript - jQuery/Grails-在文本框更改时检测表中的行

hibernate - GORM 组合 - 具有多对一关系的嵌入式域引发 org.hibernate.MappingException

hibernate - 使用别名,条件逻辑作为条件以及grails/groovy中的嵌套条件

json - 使用Groovy脚本从JSON响应中一一打印特定节点的所有值