We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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) }
The text was updated successfully, but these errors were encountered:
并不是这么简单。BOM的问题是,我们得处理文件编码。文件编码处理还是很复杂的。这些事情会带来一些问题: 1、文件编码方式很多,所以BOM头的识别规则还是很多的,所以少量代码并不不能彻底解决问题。 2、tinydom聚焦的是xml解析成dom模型。文件编码并不是其核心关注点,这样可以有效控制代码规模,避免代码过度膨胀; 3、LoadDocument本身支持io.Reader接口,所以如果确实有必要编码可以在io.Reader中处理。
Sorry, something went wrong.
不过,我后面改下文档给个处理BOM的示例
No branches or pull requests
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)
}
The text was updated successfully, but these errors were encountered: