在Go中解析相同父标签下不同标签的XML

我是 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>     


慕码人8056858
浏览 82回答 1
1回答

鸿蒙传说

使用切片可以轻松解决我的问题。下面的代码就是对应的结构体。type Level struct {&nbsp; &nbsp; Name string `xml:"name,attr"`}type Handler struct {&nbsp; &nbsp; Name string `xml:"name,attr"`}type Handlers struct {&nbsp; &nbsp; Handler []Handler `xml:"handler"`}type RootLogger struct {&nbsp; &nbsp; Level&nbsp; &nbsp;Level&nbsp; &nbsp; `xml:"level"`&nbsp; &nbsp; Handler Handlers `xml:"handlers"`}type DeploymentScanner struct {&nbsp; &nbsp; Path&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `xml:"path,attr"`&nbsp; &nbsp; RelativeTo&nbsp; &nbsp;string `xml:"relative-to,attr"`&nbsp; &nbsp; ScanInterval string `xml:"scan-interval,attr"`}type Subsystem struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xml.Name&nbsp; &nbsp; RootLogger&nbsp; &nbsp; &nbsp; &nbsp; []RootLogger&nbsp; &nbsp; &nbsp; &nbsp; `xml:"root-logger"`&nbsp; &nbsp; DeploymentScanner []DeploymentScanner `xml:"deployment-scanner"`}type Profile struct {&nbsp; &nbsp; Subsystem []Subsystem `xml:"subsystem"`}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go