英文:
Golang: Unmarshal Self Closing Tags
问题
所以我正在尝试在Google Go中解析由另一个程序生成的XML保存文件。这方面的文档非常详尽:http://golang.org/pkg/encoding/xml/#Unmarshal
但是我遇到了一个问题。保存文件中的输出如下所示:
<location id="id4" x="-736" y="-544">
<committed />
</location>
除了committed之外,location还可以是urgent或者既不是committed也不是urgent。location还可以有名称和不同的标签,但是这些似乎解析得很好。
在我的Go代码中,我使用以下结构体:
type Location struct {
Id string `xml:"id,attr"`
Committed bool `xml:"committed"`
Urgent bool `xml:"urgent"`
Labels []Label `xml:"label"`
}
尽管encoding/xml包的Unmarshal函数运行没有错误,并且数据中显示的示例也存在,但是committed和urgent的所有值都是"false"。
我应该如何更改才能获得这两个字段的正确值?
(使用以下代码片段进行解组)
xmlFile, err := os.Open("model.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
b, _ := ioutil.ReadAll(xmlFile)
var xmlScheme uppaal.UppaalXML
err = xml.Unmarshal(b, &xmlScheme)
fmt.Println(err)
英文:
So I'm trying to Unmarshal an XML file generated as a save file by another program in Google Go. It seems to be going great as the documentation on this is quite extensive: http://golang.org/pkg/encoding/xml/#Unmarshal
Still I'm running into a problem. The output in the save file is like this:
<location id="id4" x="-736" y="-544">
<committed />
</location>
Instead of committed, a location can also be urgent or neither. The locations can also have a name and different labels, but these seem to be parsing just fine.
In my Go code I'm using the following struct:
type Location struct {
Id string `xml:"id,attr"`
Committed bool `xml:"commited"`
Urgent bool `xml:"urgent"`
Labels []Label `xml:"label"`
}
And although the Unmarshal function of the encoding/xml package runs without errors and the shown example is present in the data, all the values of both committed and urgent are "false".
What should I change to get the right values for these two fields?
(Unmarshalling is done using the following piece of code)
xmlFile, err := os.Open("model.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
b, _ := ioutil.ReadAll(xmlFile)
var xmlScheme uppaal.UppaalXML
err = xml.Unmarshal(b, &xmlScheme)
fmt.Println(err)
答案1
得分: 12
根据这个讨论,不支持这种行为,你没有看到错误的唯一原因是在结构定义中拼写错误了committed
。如果你正确书写,你将会得到一个解析错误,因为一个空字符串(封闭标签的内容)不是一个布尔值(play上的示例)。
在链接的golang-nuts线程中,rsc建议使用*struct{}
(一个指向空结构体的指针),并检查该值是否为nil
(play上的示例):
type Location struct {
Id string `xml:"id,attr"`
Committed *struct{} `xml:"committed"`
Urgent bool `xml:"urgent"`
}
if l.Committed != nil {
// 处理未提交的情况
}
英文:
According to this discussion this behaviour is not supported and the only reason you don't see an error is that you mispelled committed
in the struct definition. If you write it correctly you will get a parse error because an empty string (the contents of a closed tag) is not a boolean value (example on play).
In the linked golang-nuts thread rsc suggests to use *struct{}
(a pointer to an empty struct) and check if that value is nil
(example on play):
type Location struct {
Id string `xml:"id,attr"`
Committed *struct{} `xml:"committed"`
Urgent bool `xml:"urgent"`
}
if l.Committed != nil {
// handle not committed
}
答案2
得分: 4
对于简单的布尔值,即当元素存在时值为true,我是这样解决的:
示例XML:
<struct>
<hide/>
<data>Value</data>
</struct>
Go中的数据结构:
type XMLValues struct {
Hide BoolIfElementPresent `xml:"hide"`
Data string `xml:"data"`
}
type BoolIfElementPresent struct {
bool
}
func (c *BoolIfElementPresent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
*c = BoolIfElementPresent{true}
return nil
}
这样,每当存在<hide/>
并尝试解组时,它就会返回true。如果<hide/>
不存在,则不会尝试解组,结构体中的默认值false
保持不变。
请注意,每次使用时都必须将布尔值包装在自定义结构体中。
d.DecodeElement(&v, &start)
部分似乎是不必要的,但如果省略该代码片段,您将收到错误消息:
xml: (*mypackage.BoolIfElementPresent).UnmarshalXML did not consume entire
element
**编辑:**由@ShogunPanda提供的简化版本:
type XMLValues struct {
Hide BoolIfElementPresent `xml:"hide"`
Data string `xml:"data"`
}
type BoolIfElementPresent bool
func (c *BoolIfElementPresent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
*c = true
return nil
}
请注意,您必须使用if xyz == true
来比较布尔值。
英文:
For simple boolean values, i.e. value is true when an element is present, I have solved it this way:
Example XML:
<struct>
<hide/>
<data>Value</data>
</struct>
Data structure in Go:
type XMLValues struct {
Hide BoolIfElementPresent `xml:"hide"`
Data string `xml:"data"`
}
type BoolIfElementPresent struct {
bool
}
func (c *BoolIfElementPresent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
*c = BoolIfElementPresent{true}
return nil
}
This way, whenever <hide/>
is present and unmarshalling is attempted, it just returns true. If <hide/>
is not present, no unmarshalling is attempted and the default value false
remains in the struct.
Note that you have to wrap your boolean values in the custom struct every time you work with it, though.
The d.DecodeElement(&v, &start)
part seems unnecessary, but if you omit that piece of code you will receive an error message:
> xml: (*mypackage.BoolIfElementPresent).UnmarshalXML did not consume entire <hide> element
Edit: Simplified version curtesy of @ShogunPanda:
type XMLValues struct {
Hide BoolIfElementPresent `xml:"hide"`
Data string `xml:"data"`
}
type BoolIfElementPresent bool
func (c *BoolIfElementPresent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
*c = true
return nil
}
Note that you have to compare the booleans by using if xyz == true
.
答案3
得分: 1
解决空自闭合标签或空标签的一个简单方法是使用https://github.com/guregu/null包。
我不太喜欢为小事情使用包,但是这个包为我节省了很多时间。
以下是我如何使用它的示例代码:
package main
import (
"encoding/xml"
"fmt"
"github.com/guregu/null"
)
func main() {
type Result struct {
XMLName xml.Name `xml:"Person"`
Name string `xml:"FullName"`
Id null.Int `xml:"Id"`
Height null.Float `xml:"Height"`
}
v := Result{}
data := `
<Person>
<FullName>Grace R. Emlin</FullName>
<Id></Id>
<Height />
</Person>
`
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("XMLName: %#v\n", v.XMLName)
fmt.Printf("Name: %q\n", v.Name)
if !v.Id.IsZero() {
fmt.Printf("Id: %d\n", v.Id.Int64)
}
}
http://play.golang.org/p/xGKeIUM6NO
全部功劳归给guregu/null。
英文:
An easy way to solve this issue of empty self closing tags or empty tag is to use
https://github.com/guregu/null package.
I am not a big fan of using packages for small stuff but this package have saved a lot of time for me
Here is how I am using this
package main
import (
"encoding/xml"
"fmt"
"github.com/guregu/null"
)
func main() {
type Result struct {
XMLName xml.Name `xml:"Person"`
Name string `xml:"FullName"`
Id null.Int `xml:"Id"`
Height null.Float `xml:"Height"`
}
v := Result{}
data := `
<Person>
<FullName>Grace R. Emlin</FullName>
<Id></Id>
<Height />
</Person>
`
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("XMLName: %#v\n", v.XMLName)
fmt.Printf("Name: %q\n", v.Name)
if !v.Id.IsZero() {
fmt.Printf("Id: %d\n", v.Id.Int64)
}
}
http://play.golang.org/p/xGKeIUM6NO
Full credit to guregu/null
答案4
得分: -1
我的案例有所不同:我创建了一个用于从api-a提交到api-b的golang应用程序作为转发器。
当api-a生成一个未关闭的标签时,golang将无法读取它,这将导致api-b无法读取它。因此,我创建了两个类,一个是读取器,另一个是发送器。
也许这对其他人有所启发。
package main
import (
"encoding/xml"
"fmt"
)
type MyDataReader struct {
Name string `xml:"name"`
Lulus *struct{} `xml:"lulus"`
}
type MyDataPoster struct {
Name string `xml:"name"`
Lulus string `xml:"lulus"`
}
func ToPoster(m MyDataReader) MyDataPoster {
res := MyDataPoster{}
res.Name = m.Name
if m.Lulus != nil {
res.Lulus = "1"
}
return res
}
func main() {
xmlString := `<data>
<name>richard</name>
<lulus />
</data>`
m := MyDataReader{}
err := xml.Unmarshal([]byte(xmlString), &m)
fmt.Println(err, m)
mpost := ToPoster(m)
output, errenc := xml.MarshalIndent(mpost, "", " ")
fmt.Println(errenc, string(output))
}
我创建了两个类。一个用于检索和解析,另一个用于提交XML数据。
英文:
My case is different: I create app in golang as a forwarder to submit from api-a to api-b.
While api-a may generating a tag without closing and golang will not able to read it, which will not be read by api-b. Therefore I create two classes which one is reader and other is poster(sender).
maybe this will give some insight to others.
package main
import (
"encoding/xml"
"fmt"
)
type MyDataReader struct {
Name string `xml:"name"`
Lulus *struct{} `xml:"lulus"`
}
type MyDataPoster struct {
Name string `xml:"name"`
Lulus string `xml:"lulus"`
}
func ToPoster(m MyDataReader) MyDataPoster {
res := MyDataPoster{}
res.Name = m.Name
if m.Lulus != nil {
res.Lulus = "1"
}
return res
}
func main() {
xmlString := `<data>
<name>richard</name>
<lulus />
</data>`
m := MyDataReader{}
err := xml.Unmarshal([]byte(xmlString), &m)
fmt.Println(err, m)
mpost := ToPoster(m)
output, errenc := xml.MarshalIndent(mpost, "", " ")
fmt.Println(errenc, string(output))
}
I create two class. One for retrieving and parsing and one for submit the xml.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论