Xml 在这个golang代码中可以使用更少的'struct'吗?
给定我无法控制的以下XML:Xml 在这个golang代码中可以使用更少的'struct'吗?,xml,go,Xml,Go,给定我无法控制的以下XML: <Stuff> <SomeData> <SomeDataStuff> <AccountDetails> <Person xsi:nil="true" /> <Person xsi:nil="true" /> </AccountDetails> <CandidateDetails>
<Stuff>
<SomeData>
<SomeDataStuff>
<AccountDetails>
<Person xsi:nil="true" />
<Person xsi:nil="true" />
</AccountDetails>
<CandidateDetails>
<Candidate xsi:nil="true" />
<Candidate xsi:nil="true" />
</CandidateDetails>
</SomeDataStuff>
</SomeData>
</Stuff>
我不担心编组,只担心解编组。实际上,我所需要的只是一个
Person
和Candidate
数组,而不是嵌套的无意义结构的整个序列。您可以使用选择器,例如:
// replace []string with []Person/[]Candidate
type Stuff struct {
People []string `xml:"SomeData>SomeDataStuff>AccountDetails>Person"`
Candidates []string `xml:"SomeData>SomeDataStuff>CandidateDetails>Candidate"`
}
//编辑后,我更新了示例,以显示编组也可以正常工作
发件人:
// replace []string with []Person/[]Candidate
type Stuff struct {
People []string `xml:"SomeData>SomeDataStuff>AccountDetails>Person"`
Candidates []string `xml:"SomeData>SomeDataStuff>CandidateDetails>Candidate"`
}
* If the XML element contains a sub-element whose name matches
the prefix of a tag formatted as "a" or "a>b>c", unmarshal
will descend into the XML structure looking for elements with the
given names, and will map the innermost elements to that struct
field. A tag starting with ">" is equivalent to one starting
with the field name followed by ">".