I have a zip file having several xml files in it using zip and encoding/xml packages from Go archive. The thing I want to do is unmarshalling only a.xml
into a type -i.e. without looping over all files inside:
test.zip
├ a.xml
├ b.xml
└ ...
a.xml
would have a structure like:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<app>
<code>0001</code>
<name>Some Test App</name>
</app>
<app>
<code>0002</code>
<name>Another Test App</name>
</app>
</root>
How to for select and unmarshal the file whose name is provided as a parameter in the commented out lines, for instance:
package marshalutils
import (
"archive/zip"
"log"
"fmt"
"encoding/xml"
)
type ApplicationRoot struct {
XMLName xml.Name `xml:"root"`
Applications []Application `xml:"app"`
}
type Application struct {
Code string `xml:"code"`
Name string `xml:"name"`
}
func UnmarshalApps(zipPath string, fileName string) {
// Open a zip archive for reading.
reader, err := zip.OpenReader(zipFilePath)
if err != nil {
log.Fatal(`ERROR:`, err)
}
defer reader.Close()
/*
* U N M A R S H A L T H E G I V E N F I L E ...
* ... I N T O T H E T Y P E S A B O V E
*/
}