如何在Go中处理嵌套的XML标签?

huangapple go评论86阅读模式
英文:

How to handle nested XML tags in Go?

问题

我正在尝试从蛋白质数据库(pdb)中解析查询响应。我一直在阅读Go语言的XML编码包,并了解如何处理标签,但我不知道如何处理嵌套标签。我从下面的代码中得到的输出如下:

<PDBdescription>
<PDB structureId="4HHB"....
</PDBdescription>

我该如何获取structureId的信息?因为它似乎与PDB标签相关联,而PDB标签位于PDBdescription标签内部。

// pdbRequest
package main

import (
    "fmt"
    "net"
    "encoding/xml"
    //"strings"
)

type PDB struct {
    id    string   `xml:"PDBdescription>PDB structureId"`
    XMLName xml.Name 
}

func main() {
    conn, err := net.Dial("tcp", "www.rcsb.org:http")
    p := PDB{id:"NONE"}
    if err != nil {
        return
    }
    fmt.Fprintf(conn, "GET /pdb/rest/describePDB?structureId=4hhb HTTP/1.0\r\n\r\n")
    status := make([]byte, 10000)
    conn.Read(status)
    xml.Unmarshal([]byte(status), &p)
    fmt.Println(string(status))
    fmt.Println(p.id)
}

我看到我的问题与这里的其他问题非常相似(很快会放入链接参考),但那里给出的答案似乎不是我的解决方案,因为我的标签有点不同。

英文:

I am trying to unmarshal a query response from the protein database (pdb). I have been reading on the XML encoding package of Go, and understand how to handle tags, but I do not know how to handle nested tags. I get as output from code below (cutout);

 &lt;PDBdescription&gt; 
 &lt;PDB structureId=&quot;4HHB&quot;....
 &lt;/PDBdescription&gt;

How can I get the info on the structureId? for it seems that it is connected to PDB-tag, which is within the PDBdescription-tag?

// pdbRequest
package main

import (
    &quot;fmt&quot;
    &quot;net&quot;
    &quot;encoding/xml&quot;
    //&quot;strings&quot;
)

type PDB struct {
    id    string   `xml:&quot;PDBdescription&quot;&gt;&quot;PDB structureId&quot;`
    XMLName xml.Name 
}

func main() {
    conn, err := net.Dial(&quot;tcp&quot;, &quot;www.rcsb.org:http&quot;)
    p := PDB{id:&quot;NONE&quot;}
    if err != nil {
        return
    }
    fmt.Fprintf(conn, &quot;GET /pdb/rest/describePDB?structureId=4hhb HTTP/1.0\r\n\r\n&quot;)
    status := make([]byte, 10000)
    conn.Read(status)
    xml.Unmarshal([]byte(status), &amp;p)
    fmt.Println(string(status))
    fmt.Println(p.id)
}

I see that my question is very similar to other questions here (putting in link references soon), but the answers given there seems not to be my solution because my tag is a bit different.

答案1

得分: 4

你可以在标记结构字段时使用,attr修饰符。例如:

type PDB struct {
    StructureId string `xml:"structureId,attr"`
}

type root struct {
    Pdb PDB `xml:"PDBdescription>PDB"`
}

如果你解码到一个root实例中,structureId属性将被解码到嵌套的Pdb.StructureId字段中。

不幸的是,目前无法将链式语法与,attr修饰符结合使用,所以你需要使用嵌套结构。

这里有一个可工作的示例:http://play.golang.org/p/VhUBKKLfk4

英文:

You need can use the ,attr modifier when tagging your struct field. For example:

type PDB struct {
	StructureId string `xml:&quot;structureId,attr&quot;`
}

type root struct {
	Pdb PDB `xml:&quot;PDBdescription&gt;PDB&quot;`
}

If you decode into a root instance, the structureId attribute will be decoded into the nested Pdb.StructureId field.

Unfortunately you can't combine the chaining syntax with the ,attr modifier at this point, so you will need a nested struct.

Here is a working example: http://play.golang.org/p/VhUBKKLfk4

huangapple
  • 本文由 发表于 2013年12月14日 02:28:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/20573473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定