我在 Go 中为 xml 创建了结构:
type ExceptionSorter struct {
ExceptionClass string `xml:"class-name,attr"`
}
type ValidConnection struct {
ConnClass string `xml:"class-name,attr"`
}
type Validation struct {
ValidConnection ValidConnection `xml:"ns1:valid-connection-checker"`
ExceptionSorter ExceptionSorter `xml:"ns1:exception-sorter"`
}
type DataSource struct {
Jta bool `xml:"jta,attr"`
JndiName string `xml:"jndi-name,attr"`
PoolName string `xml:"pool-name,attr"`
Enabled bool `xml:"enabled,attr"`
JavaContext bool `xml:"use-java-context,attr"`
Spy bool `xml:"spy,attr"`
UseCcm bool `xml:"use-ccm,attr"`
Connect string `xml:"ns1:datasource>ns1:connection-url"`
Class string `xml:"ns1:datasource>ns1:driver-class"`
Driver string `xml:"ns1:datasource>ns1:driver"`
MinPool int `xml:"ns1:datasource>ns1:pool>min-pool-size"`
MaxPool int `xml:"ns1:datasource>ns1:pool>max-pool-size"`
SecUser string `xml:"ns1:datasource>ns1:security>ns1:user-name"`
SecPw string `xml:"ns1:datasource>ns1:security>ns1:password"`
Validation Validation `xml:"ns1:datasource>ns1:validation"`
Timeout string `xml:"ns1:datasource>ns1:timeout,omitempty"`
Statement string `xml:"ns1:datasource>ns1:statement,omitempty"`
}
type DataSources struct {
XmlName string `xml:"xmlns:xsi,attr"`
XmlNs string `xml:"xmlns:ns1,attr"`
SchemaLocn string `xml:"xsi:schemaLocation,attr"`
DataSource DataSource `xml:"ns1:datasource"`
}
我有两个问题:
1)当我尝试对结构进行编码时,我在不期望它们的地方得到了重复项:
<DataSources ....>
<ns1:datasource ....>
<ns1:datasource>
奇怪的是,验证标签没有重复。但我看不出我处理它们的方式有什么不同。
2)我似乎找不到将命名空间放在开始标记中的方法。显然,名称不会带冒号。但是如果我添加一个 'xml.Name' 元素,起始标签也会被复制。
这是我在 Playground 中运行它的尝试:http : //play.golang.org/p/G5NvLt-ZK7
跟进:
好的,我已经想出了如何通过删除定义中的“类型”来摆脱重复项:
type datasources struct {
DataSource
}
但后来我失去了与之相关的属性:
<ns1:datasource>
相关分类