xml parsing in golang (i want to access each element in the details individually )

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

xml parsing in golang (i want to access each element in the details individually )

问题

我现在为您翻译以下内容:

我想要解析的XML数据是:

<cm>
    <id>TASK_DATA_RES</id>
    <task>
        <swid>3873-0</swid>
        <detail>
            <![CDATA[<execute name="EXECUTE">      
            <swid>3873</swid>
            <tskid>MONITOR0</tskid>
            <file_name>DiskStatusCheck.ps1</file_name>
            <param>/metricName::metric_3873_48 /metric::DiskStatusCheck /warn::1 /critical::1 /alert::1 /params::E:</param>
            <timeout></timeout>
            <user>test\\test</user>
            <passwd>test</passwd>  	<path>https://mspnocsupport.com/downloadScript.doaction=downloadAgent&amp;fileName=DiskStatusCheck.ps1&amp;version=5.00</path>
            <pathtype>local</pathtype>
            <size>9147</size>
            <encoded_size>9147</encoded_size>
            <type>POWERSHELL</type>
            <outputdir></outputdir>
            <outputfile></outputfile>
            <alert>false</alert>
            <regkeypath></regkeypath>
            <regkeyval></regkeyval>
            <process></process>
            <service></service>
            <version>5.00</version>
            <asuser_flag>0</asuser_flag>
            </execute>]]>
        </detail>
    </task>
</cm>

相应的结构用于解析这样的XML文档:

type detail struct{
    Name string `xml:"detail>name"`
    Swid string `xml:"detail>swid"`	
    Tskid string `xml:"detail>tskid"`
    File string `xml:"detail>file"`
    Param string `xml:"detail>params"`
    User string `xml:"detail>user"`   
    Passwd string `xml:"detail>passwd"`
    Path string `xml:"detail>path"`
    Pathtype string `xml:"detail>pathtype"`
    Size int `xml:"detail>size"`
    Encode string `xml:"detail>encode"`
    Type string `xml:"detail>type"`
    Outputdir string `xml:"detail>outputdir"`
    Outputfile string `xml:"detail>outputfile"`
    Alert string `xml:"detail>alert"`
    Regkeyval string `xml:"detail>regkeyval"`
    Process string `xml:"detail>process"`	
    Service string `xml:"detail>service"`
    Version float64 `xml:"detail>version"`
    Asuser_flag string `xml:"detail>asuser_flag"`
}

type task struct{
    Swid string `xml:"swid"`
    Details []detail `xml:"Details>detail"`
}

type task_data_res struct{
    ID    string `xml:"id"`
    Tasks []task `xml:"Tasks>task"`	
}

v := task_data_res{}
err := xml.Unmarshal([]byte(*dxml), &v)
if err != nil {
    fmt.Printf("error: %v", err)
    return
}

之后,我想要打印Detail结构的各个值,例如name、Swid等。

英文:

The XML data that i want to parse is:

&lt;cm&gt;
&lt;id&gt;TASK_DATA_RES&lt;/id&gt;
&lt;task&gt;
	&lt;swid&gt;3873-0&lt;/swid&gt;
     &lt;detail&gt;
	&lt;![CDATA[&lt;execute name=&quot;EXECUTE&quot;&gt;      
	&lt;swid&gt;3873&lt;/swid&gt;
	&lt;tskid&gt;MONITOR0&lt;/tskid&gt;
	&lt;file_name&gt;DiskStatusCheck.ps1&lt;/file_name&gt;
	&lt;param&gt;/metricName::metric_3873_48 /metric::DiskStatusCheck /warn::1 /critical::1 /alert::1 /params::E:&lt;/param&gt;
	&lt;timeout&gt;&lt;/timeout&gt;
	&lt;user&gt;test\\test&lt;/user&gt;
	&lt;passwd&gt;test&lt;/passwd&gt;  	&lt;path&gt;https://mspnocsupport.com/downloadScript.doaction=downloadAgent&amp;fileName=DiskStatusCheck.ps1&amp;version=5.00&lt;/path&gt;
	&lt;pathtype&gt;local&lt;/pathtype&gt;
	&lt;size&gt;9147&lt;/size&gt;
	&lt;encoded_size&gt;9147&lt;/encoded_size&gt;
	&lt;type&gt;POWERSHELL&lt;/type&gt;
	&lt;outputdir&gt;&lt;/outputdir&gt;
	&lt;outputfile&gt;&lt;/outputfile&gt;
	&lt;alert&gt;false&lt;/alert&gt;
	&lt;regkeypath&gt;&lt;/regkeypath&gt;
	&lt;regkeyval&gt;&lt;/regkeyval&gt;
	&lt;process&gt;&lt;/process&gt;
	&lt;service&gt;&lt;/service&gt;
	&lt;version&gt;5.00&lt;/version&gt;
	&lt;asuser_flag&gt;0&lt;/asuser_flag&gt;
	&lt;/execute&gt;]]&gt;
    &lt;/detail&gt;
&lt;/task&gt;
&lt;/cm&gt;

The corresponding structure to parse such XML documents:

type detail struct{
    Name string `xml:&quot;detail&gt;name&quot;`
	Swid string `xml:&quot;detail&gt;swid&quot;`	
	Tskid string `xml:&quot;detail&gt;tskid&quot;`
	File string `xml:&quot;detail&gt;file&quot;`
	Param string `xml:&quot;detail&gt;params&quot;`
	User string `xml:&quot;detail&gt;user&quot;`   
	Passwd string `xml:&quot;detail&gt;passwd&quot;`
	Path string `xml:&quot;detail&gt;path&quot;`
	Pathtype string `xml:&quot;detail&gt;pathtype&quot;`
	Size int `xml:&quot;detail&gt;size&quot;`
	Encode string `xml:&quot;detail&gt;encode&quot;`
	Type string `xml:&quot;detail&gt;type&quot;`
	Outputdir string `xml:&quot;detail&gt;outputdir&quot;`
	Outputfile string `xml:&quot;detail&gt;outputfile&quot;`
	Alert string `xml:&quot;detail&gt;alert&quot;`
	Regkeyval string `xml:&quot;detail&gt;regkeyval&quot;`
	Process string `xml:&quot;detail&gt;process&quot;`	
	Service string `xml:&quot;detail&gt;service&quot;`
	Version float64 `xml:&quot;detail&gt;version&quot;`
	Asuser_flag string `xml:&quot;detail&gt;asuser_flag&quot;`
}

type task struct{
	Swid string `xml:&quot;swid&quot;`
	Details []detail `xml:&quot;Details&gt;detail&quot;`
}
type task_data_res struct{
	ID    string `xml:&quot;id&quot;`
	//Swid  string `xml:&quot;task&gt;swid&quot;`
	Tasks []task `xml:&quot;Tasks&gt;task&quot;`	
}
v := task_data_res{}
err := xml.Unmarshal([]byte(*dxml), &amp;v)
if err != nil {
	fmt.Printf(&quot;error: %v&quot;, err)
	return
}

After that i want to print individual value of Detail structure such as name,Swid etc.

答案1

得分: -1

我已经尝试解析你的 XML,你可以在 http://play.golang.org/p/1UCaGYetzo 找到完整的解决方案。

英文:

I have tried to parse your xml, you can find complete solution at http://play.golang.org/p/1UCaGYetzo

huangapple
  • 本文由 发表于 2015年11月5日 20:43:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/33544879.html
匿名

发表评论

匿名网友

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

确定