如何在使用Go进行XML文件的解组时获取更多的错误详细信息?

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

How to get more error detailing when doing Go's unmarshalling for XML files?

问题

我对Golang还比较新,但学得相当快。我正在开发一个涉及解析XML表示的模式的工具。直观地说,我一直在使用Go标准库中的xml.Unmarshal来避免编写自己的解析器。

然而,当解析函数失败时,我注意到它并没有详细说明失败的位置或行数。如果XML模式有几百行,这就会带来很多麻烦。

下面的代码是我从文件中读取XML的方式,它似乎是Go中读取XML文件的一种标准方式。

	xmlFile, err := os.Open(file)
	if err != nil {
		fmt.Println("Error attempting to open file: ", err)
		return
	}

	defer xmlFile.Close()

	var schema xmlschema.TFP

	// Read in the bytes
	byteValue, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Read in the data into the structure
	err = xml.Unmarshal(byteValue, &schema)

	if err != nil {
		fmt.Println(err)
		return
	}

当然,考虑到我对Go还不熟悉,也许这不是读取XML文件的最佳方式。

有没有办法让Go的xml/encoding库显示更多关于无法解析文件的详细信息?如果没有,有什么第三方的替代方案呢?

英文:

I'm pretty new to Golang, but picking things up pretty quickly. I'm developing a tool that involves parsing schemas represented in XML. Intuitively, I've been using Go's xml.Unmarshal from the standard library to avoid having to write my own parser.

However, when the unmarshal function fails, I noticed that it doesn't really go into detail on where or what line it failed. This is pretty troublesome if the XML schema is hundred of lines.

The code below is how I read XML from files and it appears to be a pretty standard way to read in XML files in Go.

	xmlFile, err := os.Open(file)
	if err != nil {
		fmt.Println("Error attempting to open file: ", err)
		return
	}

	defer xmlFile.Close()

	var schema xmlschema.TFP

	// Read in the bytes
	byteValue, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Read in the data into the structure
	err = xml.Unmarshal(byteValue, &schema)

	if err != nil {
		fmt.Println(err)
		return
	}

Of course, given that I'm new to Go, maybe this isn't the best way to read in XML files.

Is there a way to have Go's xml/encoding library display more details on why it wasn't able to unmarshal the file? If not, what would be 3rd party alternatives?

答案1

得分: 1

根据我在使用Go和XML时的经验,似乎没有办法从xml.Unmarshal()方法返回的任何错误中获取更多的调试信息。

我不确定你遇到了什么错误,但请记住,Go库仅支持XML版本1.0。另外请参考:https://pkg.go.dev/encoding/xml#pkg-note-BUG

不幸的是,与对JSON的支持相比,Go对XML解析的支持并不是非常出色。如果可能的话,我建议尝试将输入数据格式化为JSON,以避免许多麻烦。第三方库对XML解析的支持也比较有限。

我见过一些像https://github.com/beevik/etree这样的库,可能会对你有所帮助。不过,目前Go的XML解析器的活跃开发相对较少。

英文:

From what I have seen working with go and xml, there is not a way to get more debug info logged out from any errors that the xml.Unmarshal() method returns.

I am not sure what error you are seeing, but remember that the go library is built to support XML version 1.0 only. Also: https://pkg.go.dev/encoding/xml#pkg-note-BUG

Unfortunately go's support for xml parsing is not super great compared to json support. I would suggest trying to get your input data formatted in something like json if possible to avoid a lot of headache. The third-party package support for xml parsing is also lacking.

I have seen things like https://github.com/beevik/etree that might interest you. There is little active development on XML parsers for Go though...

huangapple
  • 本文由 发表于 2022年4月9日 03:52:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71802511.html
匿名

发表评论

匿名网友

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

确定