在Go语言中解析具有相同父标签的不同标签的XML。

huangapple go评论83阅读模式
英文:

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:

&lt;profile&gt;
    &lt;subsystem xmlns=&quot;urn:jboss:domain:logging:1.1&quot;&gt;
            &lt;root-logger&gt;
                &lt;level name=&quot;INFO&quot;/&gt;
                &lt;handlers&gt;
                    &lt;handler name=&quot;CONSOLE&quot;/&gt;
                    &lt;handler name=&quot;FILE&quot;/&gt;
                &lt;/handlers&gt;
            &lt;/root-logger&gt;
    &lt;/subsystem&gt;
    &lt;subsystem xmlns=&quot;urn:jboss:domain:configadmin:1.0&quot;/&gt;
    &lt;subsystem xmlns=&quot;urn:jboss:domain:deployment-scanner:1.1&quot;&gt;
            &lt;deployment-scanner path=&quot;deployments&quot; relative-to=&quot;jboss.server.base.dir&quot; scan-interval=&quot;5000&quot;/&gt;
    &lt;/subsystem&gt;
 &lt;/profile&gt;     

答案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"`
}

Go Playground

英文:

My problem can be easily solved by using slices.
The following code is the corresponding structure.

type Level struct {
	Name string `xml:&quot;name,attr&quot;`
}
type Handler struct {
	Name string `xml:&quot;name,attr&quot;`
}
type Handlers struct {
	Handler []Handler `xml:&quot;handler&quot;`
}

type RootLogger struct {
	Level   Level    `xml:&quot;level&quot;`
	Handler Handlers `xml:&quot;handlers&quot;`
}

type DeploymentScanner struct {
	Path         string `xml:&quot;path,attr&quot;`
	RelativeTo   string `xml:&quot;relative-to,attr&quot;`
	ScanInterval string `xml:&quot;scan-interval,attr&quot;`
}

type Subsystem struct {
	XMLName           xml.Name
	RootLogger        []RootLogger        `xml:&quot;root-logger&quot;`
	DeploymentScanner []DeploymentScanner `xml:&quot;deployment-scanner&quot;`
}

type Profile struct {
	Subsystem []Subsystem `xml:&quot;subsystem&quot;`
}

Go Playground

huangapple
  • 本文由 发表于 2022年5月12日 01:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/72205120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定