解析前导XML注释
我想处理一个配置文件。此文件应由应用程序读取和写入。配置文件应包含注释,以提供有关配置标记的信息 标签中的注释没有问题。我使用解析前导XML注释,xml,go,comments,Xml,Go,Comments,我想处理一个配置文件。此文件应由应用程序读取和写入。配置文件应包含注释,以提供有关配置标记的信息 标签中的注释没有问题。我使用'xml:,comment“标记。 但是我无法获得标记之外的注释 <?xml version="1.0" encoding="UTF-8"?> <!-- This is a comment I cannot get --> <ServerConfig> <!-- This is a co
'xml:,comment“
标记。
但是我无法获得
标记之外的注释
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a comment I cannot get -->
<ServerConfig>
<!-- This is a comment I can get -->
<KeyStore>/tmp/test</KeyStore>
</ServerConfig>
如何才能Unmarshal()
主要注释?您可以使用它逐行读取xml
并实现自定义解析算法
const configXml =`<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a comment I cannot get -->
<ServerConfig>
<!-- This is a comment I can get -->
<KeyStore>/tmp/test</KeyStore>
</ServerConfig>`
请注意,您提供的xml和Go代码都是无效的。xml缺少正确的结束标记,Go代码正在使用
“
作为标记分隔符,这是非法的。您无法分析xml基本标记之外的注释,解码器从init标记加载标记。请调整xml和Go代码以使其正确,抱歉
const configXml =`<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a comment I cannot get -->
<ServerConfig>
<!-- This is a comment I can get -->
<KeyStore>/tmp/test</KeyStore>
</ServerConfig>`
dec := xml.NewDecoder(strings.NewReader(configXml))
for{
tok, err := dec.Token()
if err != nil && err != io.EOF {
panic(err)
} else if err == io.EOF {
break
}
if tok == nil {
fmt.Println("token is nil")
}
switch toke := tok.(type) {
case xml.Comment:
fmt.Println(string(toke))
}
}