英文:
Getting root struct after xml unmarshal
问题
我正在阅读一个 XML 文件并自动进行解组。
我定义了以下数据结构:
type oDoc struct {
Body oBody `xml:"body"`
AutoStyle oAutoStyle `xml:"automatic-styles"`
}
type oBody struct {
Spreadsheet oSpread `xml:"spreadsheet"`
}
type oSpread struct {
Tables []oTable `xml:"table"`
}
type oTable struct {
Name string `xml:"name,attr"`
Rows []oRow `xml:"table-row"`
}
type oRow struct {
Cells []oCell `xml:"table-cell"`
Style string `xml:"style-name,attr"`
}
下面还有更多内容,但对于这个示例来说并不重要。
从 oRow 对象中,我需要访问根 oDoc 对象。
这可能吗?我看到了几个使用接口的示例,但这似乎要求我手动添加每个元素以设置相应的父元素。我不确定我能否这样做,因为解组是自动进行的。
编辑:我想要实现的示例。oDoc 分为 oTables 和 oStyles(为简洁起见,未添加样式)。每个 oRow 都有一个与 oStyle 对象对应的样式名称。我想要创建一个方法,可以执行以下操作:
rowOject.getStyleObject()
根据 gonutz 的建议,我可以这样做:
docObj.getRow(specificRow).getStyle(docObj)
并使用该 docObj 来深入到我想要的样式,但这似乎不是一个好的方式。如果这是唯一/最好的解决方案,我会采用它,但似乎应该有更好的方法。
有什么建议吗?
英文:
I'm reading an xml file and automatically unmarshalling it.
I defined the data structure as follows:
type oDoc struct {
Body oBody `xml:"body"`
AutoStyle oAutoStyle `xml:"automatic-styles"`
}
type oBody struct {
Spreadsheet oSpread `xml:"spreadsheet"`
}
type oSpread struct {
Tables []oTable `xml:"table"`
}
type oTable struct {
Name string `xml:"name,attr"`
Rows []oRow `xml:"table-row"`
}
type oRow struct {
Cells []oCell `xml:"table-cell"`
Style string `xml:"style-name,attr"`
}
There is more further down but it doesn't matter for this example.
From an oRow object, I need to access the root oDoc object.
Is this possible? I've seen several examples using interfaces but this seems to require me manually adding each element to set the respective parent. I'm not sure I can do this as the unmarshalling is automatic.
Edit: Example of what I'm trying to achieve. oDoc splits into oTables and oStyles (styles not added for brevity). Each oRow has a style Name corresponding to an oStyle object. I want to be able to create a method that can do
rowOject.getStyleObject()
As per gonutz's suggestion, I could do something like
docObj.getRow(specificRow).getStyle(docObj)
and use that docObj to drilldown to the style I want but this like it is bad form. If it's the only/best solution, I'll go for it but seems like there should be a better way.
Any suggestions?
答案1
得分: 2
只需在文档中添加反向引用,如果确实需要它们。以下是代码所需的更改:
type oRow struct {
Cells []oCell `xml:"table-cell"`
Style string `xml:"style-name,attr"`
doc *oDoc // 这不会影响xml解析
}
func main() {
var doc oDoc
// 加载oDoc...
// 然后添加反向引用
for t := range doc.Body.Spreadsheet.Tables {
table := &doc.Body.Spreadsheet.Tables[t]
for i := range table.Rows {
table.Rows[i].doc = &doc
}
}
}
英文:
Just add back-references to your document if you really need them. Here are the needed changes to the code:
type oRow struct {
Cells []oCell `xml:"table-cell"`
Style string `xml:"style-name,attr"`
doc *oDoc // this will not affect the xml parsing
}
func main() {
var doc oDoc
// load the oDoc...
// then add the back-references
for t := range doc.Body.Spreadsheet.Tables {
table := &doc.Body.Spreadsheet.Tables[t]
for i := range table.Rows {
table.Rows[i].doc = &doc
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论