英文:
Parsing XML of the different tag under same parent tags in Go
问题
我是新手,正在尝试解析一个XML文件。我不知道如何将下面这样的XML转换为结构体。
我的XML文件:
<profile>
<subsystem xmlns="urn:jboss:domain:logging:1.1">
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
</subsystem>
<subsystem xmlns="urn:jboss:domain:configadmin:1.0"/>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">
<deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000"/>
</subsystem>
</profile>
请注意,我只提供翻译服务,不会回答与翻译无关的问题。
英文:
I am new to Go, and I am trying to parse an XML file. I don't know how to convert the xml like below into a struct.
My XML file:
<profile>
<subsystem xmlns="urn:jboss:domain:logging:1.1">
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
</subsystem>
<subsystem xmlns="urn:jboss:domain:configadmin:1.0"/>
<subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">
<deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000"/>
</subsystem>
</profile>
答案1
得分: 0
我的问题可以通过使用切片轻松解决。
以下代码是相应的结构。
type Level struct {
Name string `xml:"name,attr"`
}
type Handler struct {
Name string `xml:"name,attr"`
}
type Handlers struct {
Handler []Handler `xml:"handler"`
}
type RootLogger struct {
Level Level `xml:"level"`
Handler Handlers `xml:"handlers"`
}
type DeploymentScanner struct {
Path string `xml:"path,attr"`
RelativeTo string `xml:"relative-to,attr"`
ScanInterval string `xml:"scan-interval,attr"`
}
type Subsystem struct {
XMLName xml.Name
RootLogger []RootLogger `xml:"root-logger"`
DeploymentScanner []DeploymentScanner `xml:"deployment-scanner"`
}
type Profile struct {
Subsystem []Subsystem `xml:"subsystem"`
}
英文:
My problem can be easily solved by using slices.
The following code is the corresponding structure.
type Level struct {
Name string `xml:"name,attr"`
}
type Handler struct {
Name string `xml:"name,attr"`
}
type Handlers struct {
Handler []Handler `xml:"handler"`
}
type RootLogger struct {
Level Level `xml:"level"`
Handler Handlers `xml:"handlers"`
}
type DeploymentScanner struct {
Path string `xml:"path,attr"`
RelativeTo string `xml:"relative-to,attr"`
ScanInterval string `xml:"scan-interval,attr"`
}
type Subsystem struct {
XMLName xml.Name
RootLogger []RootLogger `xml:"root-logger"`
DeploymentScanner []DeploymentScanner `xml:"deployment-scanner"`
}
type Profile struct {
Subsystem []Subsystem `xml:"subsystem"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论