Parsing XML attributes with GO

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

Parsing XML attributes with GO

问题

我对GO还不太熟悉,我在从XML文档中提取属性值方面遇到了一些问题。下面的代码产生了以下输出:

应用程序ID:""
应用程序名称:""

我猜想在使用标记时我可能遗漏了一些东西,如果有人能指点我正确的方向,我将不胜感激。

data:=<?xml version="1.0" encoding="UTF-8"?> <applist> <app app_id="1234" app_name="abc"/> <app app_id="5678" app_name="def"/> </applist>

type App struct {
app_id string xml:"app_id,attr"
app_name string xml:"app_name"
}

type AppList struct {
XMLName xml.Name xml:"applist"
Apps []App xml:"app"
}

var portfolio AppList
err := xml.Unmarshal([]byte(data), &portfolio)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("application ID:: %q\n", portfolio.Apps[0].app_id)
fmt.Printf("application name:: %q\n", portfolio.Apps[0].app_name)

英文:

I am pretty new to GO and I am having trouble extracting attribute values from XML documents. The code below produces the following output:

application ID:: ""
application name:: ""

My assumption is that I am missing something when it comes to how to use tagging and I would really appreciate it if someone could point me in the right direction.

data:=`<?xml version="1.0" encoding="UTF-8"?>
    <applist>
        <app app_id="1234" app_name="abc"/>
    <app app_id="5678" app_name="def"/>
    </applist> `

type App struct {
	app_id   string  `xml:"app_id,attr"`
	app_name string  `xml:"app_name"`
}

type AppList struct {
	XMLName xml.Name `xml:"applist"`
	Apps  []App      `xml:"app"`
}

var portfolio AppList
err := xml.Unmarshal([]byte(data), &portfolio)
if err != nil {
	fmt.Printf("error: %v", err)
	return
}
fmt.Printf("application ID:: %q\n", portfolio.Apps[0].app_id)
fmt.Printf("application name:: %q\n", portfolio.Apps[0].app_name)

答案1

得分: 5

为了能够提取元素,你需要在App结构体中将app_idapp_name字段定义为"导出"字段,也就是说它们的首字母应该大写。此外,你的app_name字段在xml字段标签中还缺少了,attr。以下是你的代码的一个可工作示例,我已经在需要更改的行上添加了注释。

package main

import (
	"fmt"
	"encoding/xml"
)

func main() {
	data := `
        <?xml version="1.0" encoding="UTF-8"?>
        <applist>
            <app app_id="1234" app_name="abc"/>
            <app app_id="5678" app_name="def"/>
        </applist>
    `

    type App struct {
        App_id   string `xml:"app_id,attr"`    // 注意这里的字段名首字母大写
        App_name string `xml:"app_name,attr"`  // 注意这里的字段名首字母大写和`xml:"app_name,attr"`
    }

    type AppList struct {
        XMLName xml.Name `xml:"applist"`
        Apps    []App    `xml:"app"`
    }

    var portfolio AppList
    err := xml.Unmarshal([]byte(data), &portfolio)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    fmt.Printf("application ID: %q\n", portfolio.Apps[0].App_id)       // 对应App的更改
    fmt.Printf("application name: %q\n", portfolio.Apps[0].App_name)   // 对应App的更改
}

希望对你有帮助!

英文:

In order to be able to get the elements out you have to have "exported" fields, meaning that app_id and app_name in the App struct should start with a capital letter. In addition, your app_name field is also missing a ,attr in its xml field tag. See below for a working example of your code. I've added comments on the lines that require some changes.

package main

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

func main() {
	data:=`
    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;applist&gt;
        &lt;app app_id=&quot;1234&quot; app_name=&quot;abc&quot;/&gt;
        &lt;app app_id=&quot;5678&quot; app_name=&quot;def&quot;/&gt;
    &lt;/applist&gt;
    `

    type App struct {
        App_id   string  `xml:&quot;app_id,attr&quot;`    // notice the capitalized field name here
        App_name string  `xml:&quot;app_name,attr&quot;`  // notice the capitalized field name here and the `xml:&quot;app_name,attr&quot;`
    }

    type AppList struct {
        XMLName xml.Name `xml:&quot;applist&quot;`
        Apps  []App      `xml:&quot;app&quot;`
    }

    var portfolio AppList
    err := xml.Unmarshal([]byte(data), &amp;portfolio)
    if err != nil {
        fmt.Printf(&quot;error: %v&quot;, err)
        return
    }
    fmt.Printf(&quot;application ID:: %q\n&quot;, portfolio.Apps[0].App_id)       // the corresponding changes here for App
    fmt.Printf(&quot;application name:: %q\n&quot;, portfolio.Apps[0].App_name)   // the corresponding changes here for App
}

huangapple
  • 本文由 发表于 2016年4月28日 21:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/36916504.html
匿名

发表评论

匿名网友

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

确定