groovy XmlSlurper不解析我的xml文件

Fab*_*ier 2 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>
Run Code Online (Sandbox Code Playgroud)

我只需要从中获取文本 <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
}
Run Code Online (Sandbox Code Playgroud)

我的错误信息:

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 
Run Code Online (Sandbox Code Playgroud)

我不知道我的错在哪里?

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

有人有想法吗?谢谢

Jea*_*ash 5

您无需取消引用顶部标记(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}]"
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您的错误消息是由于尝试使用带有破折号的属性名称引起的.

  • 是的,你是绝对正确的.只是为了澄清:`eFetchResult.Entrezgene-Set.Entrezgene`被Groovy解释为`eFetchResult.Entrezgene - Set.Entrezgene`,因此出现错误消息(类Set的属性为Entrezgene),所以把它放在引号中会解决这个问题.但是,正如Jean指出的那样,根XML元素不必(实际上,可能不)包含在GPath中. (2认同)