英文:
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&fileName=DiskStatusCheck.ps1&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:
<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&fileName=DiskStatusCheck.ps1&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>
The corresponding structure to parse such XML documents:
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"`
//Swid string `xml:"task>swid"`
Tasks []task `xml:"Tasks>task"`
}
v := task_data_res{}
err := xml.Unmarshal([]byte(*dxml), &v)
if err != nil {
fmt.Printf("error: %v", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论