代码之家  ›  专栏  ›  技术社区  ›  Fabien Barbier

groovy xmlslurper不分析我的XML文件

  •  2
  • Fabien Barbier  · 技术社区  · 15 年前

    我有一个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=efetcheresult./enterzgene set/.enterzgene.enterzgene_summary

    有人有主意吗? 谢谢

    2 回复  |  直到 15 年前
        1
  •  5
  •   Jean Barmash    15 年前

    您不需要取消对顶部标记的引用(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}]"
    

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

        2
  •  2
  •   Fabien Barbier    15 年前

    谢谢您, 我只是在你的帮助下解决我的问题:

    • 如果我的xml元素中有连字符(例如:result.test=efetcheresult.enterzgene.'enterzgene'track-info.'gene-track.'gene-track'geneid'),
    • 通过删除,顶部标记引用(如果保留顶部标记引用,则我的映射值为空-很好地知道:—)

    这里是我的修复:

      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
    }
    
    推荐文章