Go XML错误:无效的字符实体

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

Go XML error: invalid character entity

问题

Go无法解析带有声明实体的正确xml文件,一直出现以下错误:

错误:第47行的XML语法错误:无效的字符实体&n;

该行代码为<pos>&n;</pos>,实体定义为<!ENTITY n "noun (common) (futsuumeishi)">

这是Go中的程序:http://play.golang.org/p/94_60srVne

英文:

Go can't parse a correct xml file with declared entities, keep getting this error:

> error: XML syntax error on line 47: invalid character entity &n;

The line being <pos>&n;</pos> and the entity defined as <!ENTITY n "noun (common) (futsuumeishi)">

Here is the program in Go: http://play.golang.org/p/94_60srVne

答案1

得分: 6

你可以在创建Decoder并修改其Entity映射时传递实体。我怀疑该包实际上并没有解析DTD,只是从查看xml.go文件中的内容来看;我看到有一个注释说它会为调用者累积实体,但没有看到它自己在d.Entity中设置条目的内容。

(即使是encoding/xml安全地提供这个功能也很棘手,因为有一个内置的共享HTML实体映射。更新一个文档的映射会影响其他文档的解析。)

创建具有自定义实体的解码器需要比常规的xml.Unmarshal多一些工作,但并不复杂:

func main() {
    jmd := JMdict{}

    d := xml.NewDecoder(bytes.NewReader([]byte(str)))
    d.Entity = map[string]string{
        "n": "(noun)",
    }
    err := d.Decode(&jmd)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    fmt.Println(jmd)
}

这里是一个Playground链接,其中包含Entity技巧和一些输出代码,以将对象显示为JSON格式。

英文:

You can pass entities in if you create a Decoder and mess with its Entity map. I suspect the package doesn't actually parse DTDs, just from poking around xml.go; I see a comment saying it accumulates entities for the caller, but nothing that itself sets entries in d.Entity.

(It would be tricky for encoding/xml to safely provide that, even, because there is a built-in shared HTML entity map. Updating it for one doc would affect parsing of others.)

There's a little more paperwork to create a Decoder with custom entities than there is for regular xml.Unmarshal, but not too much:

func main() {
	jmd := JMdict{}

	d := xml.NewDecoder(bytes.NewReader([]byte(str)))
	d.Entity = map[string]string{
		"n": "(noun)",
	}
	err := d.Decode(&jmd)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	fmt.Println(jmd)
}

Here's a Playground link with the Entity trick and some output code to show the object as JSON.

答案2

得分: 4

之前的答案是“正确”的答案,但是我相信,根据你真正想要实现的目标,一个“快速”的答案是禁用Strict。例如:

d := xml.NewDecoder(os.Stdin)
d.Strict = false
英文:

The previous answer is the "right" answer, but I believe, depending on what you are really trying to accomplish, a "quick" answer is to disable Strict. e.g.:

d := xml.NewDecoder(os.Stdin)
d.Strict = false            

huangapple
  • 本文由 发表于 2015年2月23日 03:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/28662417.html
匿名

发表评论

匿名网友

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

确定