Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于bom的支持,参考如下代码 #2

Open
mg-lee opened this issue Mar 27, 2019 · 2 comments
Open

关于bom的支持,参考如下代码 #2

mg-lee opened this issue Mar 27, 2019 · 2 comments

Comments

@mg-lee
Copy link

mg-lee commented Mar 27, 2019

func LoadDocumentFromFile(name string) (XMLDocument, error) {
file, err := os.Open(name)
if nil != err {
return nil, err
}
defer file.Close()
//读取三个字节,判断是否未bom
b := make([]byte, 3 ,3)
_, err1 := file.Read(b)
if err1 != nil{
return nil, err1
}
if !((b[0] == 239) && (b[1] == 187) && (b[2] == 191)){
//非bom移回到文件头位置
file.Seek(0, 0)
}
return LoadDocument(file)
}

@libbylg
Copy link
Collaborator

libbylg commented Apr 22, 2019

并不是这么简单。BOM的问题是,我们得处理文件编码。文件编码处理还是很复杂的。这些事情会带来一些问题:
1、文件编码方式很多,所以BOM头的识别规则还是很多的,所以少量代码并不不能彻底解决问题。
2、tinydom聚焦的是xml解析成dom模型。文件编码并不是其核心关注点,这样可以有效控制代码规模,避免代码过度膨胀;
3、LoadDocument本身支持io.Reader接口,所以如果确实有必要编码可以在io.Reader中处理。

@libbylg
Copy link
Collaborator

libbylg commented Apr 22, 2019

不过,我后面改下文档给个处理BOM的示例

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants