在 GO 中解析 Xml 以获取具有“:"的属性在标签中 [英] Parse Xml in GO for atttribute with ":" in tag

查看:19
本文介绍了在 GO 中解析 Xml 以获取具有“:"的属性在标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个 XML 文件的属性.它适用于任何普通"属性,例如 <application name="AppName">

I want to parse the attribute of an Xml file. It works for any "normal" attributes like for instance <application name="AppName">

但如果属性中包含:",我将无法检索该属性的值.例如 <application name:test="AppName">

But I can't manage to retrieve the value of the attribute if it has a ":" in it., like <application name:test="AppName">

这是我用来解析这个的代码:

Here is the code that I am using to parse this :

package main

import "fmt"
import "encoding/xml"

type Application struct {
    Event Event `xml:"application"`
    Package   string    `xml:"package,attr"`
}

type Event struct {
    IsValid   string `xml:"test:isValid,attr"`
}

var doc = []byte(`<?xml version="1.0" encoding="utf-8" standalone="no"?>
    <application package="leNomDuPackage">
            <event test:isValid="true">
        </application>
</manifest>`)

func main() {
    application := Application{}
    xml.Unmarshal(doc, &application)

    fmt.Println("Application:  ", application)
    fmt.Println("isValid:", application.Event)
}

你也可以在 Golang 操场上找到它:[https://play.golang.org/p/R6H80xPezhm

You can find it on the Golang playground as well : [https://play.golang.org/p/R6H80xPezhm

我想检索 isValid 属性的值.

I want to retrieve the value of the isValid attribute.

目前,我收到该错误消息,但无法解决.

At the moment, I have that error message and I can't manage to solve it.

struct field tag xml:"test:isValid,attr 与 reflect.StructTag.Get 不兼容:struct tag value 语法错误

struct field tag xml:"test:isValid,attr not compatible with reflect.StructTag.Get: bad syntax for struct tag value

我也尝试了以下值

type Event struct {
    IsValid   string `xml:"test isValid,attr`
}

type Event struct {
    IsValid   string `xml:"test:isValid,attr`
}

但它并不能正常工作.

推荐答案

您可以在标签定义中省略 "test:" 前缀.只需确保您的 XML 有效,您的 XML 没有 <event> 的结束标记并且有一个不匹配的结束标记 </manifest>.您还缺少标记定义中的右引号.

You can leave out the "test:" prefix in the tag definition. Just make sure your XML is valid, yours don't have a closing tag for <event> and has an unmatched closing tag </manifest>. You're also missing a closing quotation mark in the tag definition.

型号:

type Application struct {
    Event   Event  `xml:"event"`
    Package string `xml:"package,attr"`
}

type Event struct {
    IsValid string `xml:"isValid,attr"`
}

一个有效的 XML 示例:

A valid XML example:

var doc = `
<application package="leNomDuPackage">
    <event test:isValid="true" />
</application>`

代码解析:

application := Application{}
err := xml.Unmarshal([]byte(doc), &application)
if err != nil {
    fmt.Println(err)
}

fmt.Printf("Application: %#v
", application)

输出(在 Go Playground 上试试):

Output (try it on the Go Playground):

Application: main.Application{Event:main.Event{IsValid:"true"},
    Package:"leNomDuPackage"}

注意,如果你有多个同名但前缀不同的属性,比如这个例子:

Note that if you have multiple attributes with the same name but different prefixes, such as this example:

var doc = `
<application package="leNomDuPackage">
    <event test:isValid="true" test2:isValid="false" />
</application>`

然后你可以在标签值中添加命名空间前缀,名称之间用空格隔开,如下所示:

Then you may add the namespace prefix in the tag value, separated by a space from the name, like this:

type Event struct {
    IsValid1 string `xml:"test isValid,attr"`
    IsValid2 string `xml:"test2 isValid,attr"`
}

解析代码是一样的.输出(在 Go Playground 上试试):

Parsing code is the same. Output (try it on the Go Playground):

Application: main.Application{Event:main.Event{IsValid1:"true", 
    IsValid2:"false"}, Package:"leNomDuPackage"}

这篇关于在 GO 中解析 Xml 以获取具有“:"的属性在标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆