Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析前导XML注释_Xml_Go_Comments - Fatal编程技术网

解析前导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))
    }
}