views:

560

answers:

2

I have an xml, and I can't parse this file with xmlslurper. Here a copy of my xml file :

<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>

I just need to get text from <Entrezgene_summary>

Here my code :

  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
}

And my error message :

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

I don't see where is my fault?

I also try : result.geneSummary = eFetchResult./Entrezgene-Set/.Entrezgene.Entrezgene_summary

Someone has an idea? Thanks

+5  A: 

You don't need to dereference the top tag (Entersgene-Set>). The following works for me in 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}]"

BTW, your error message is caused by trying to use a property name with a dash in it.

Jean Barmash
Yep, you are absolutely correct. Just to clarify: `eFetchResult.Entrezgene-Set.Entrezgene` is interpreted by Groovy as `eFetchResult.Entrezgene - Set.Entrezgene`, thus the error message (No such property Entrezgene for class Set), so putting it in quotes would fix this. However, as Jean pointed out, the root XML element doesn't have to (actually, may not) be included in the GPath.
Daniel Rinser
+1  A: 

Thank you, I just fix my problem with your help :

  • by using quotes, if there is an hyphen in my xml element (ex : result.test = eFetchResult.Entrezgene.'Entrezgene_track-info'.'Gene-track'.'Gene-track_geneid'),
  • by delete, the top tag reference (if I keep the top tag reference, my map values are empty - it's good to know that :-)

Here my fix :

  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.Entrezgene_summary
  }
  else{
    log.error("PubmedEfetchParserService.PubmedEsearch FAILED")
    log.error(url)
    log.error(connection.responseCode)
    log.error(connection.responseMessage)
  }
  render result
}
Fabien Barbier
If you have a property name with a dash in it, the usual way round it is to single quote the property name, ie: `eFetchResult.'Entrezgene-set'.Entrezgene` :-)
tim_yates