51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package collection
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"os"
|
|
)
|
|
|
|
// ReadFileAndConvertToCollection reads a file and converts it to the Collection struct
|
|
func ReadFileAndConvertToCollection(filePath string) (*NML, error) {
|
|
// Read the file content
|
|
fileContent, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Unmarshal the XML content into the Collection struct
|
|
var collection NML
|
|
err = xml.Unmarshal(fileContent, &collection)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &collection, nil
|
|
}
|
|
func CollectionFromString(content string) (*NML, error) {
|
|
// Unmarshal the XML content into the Collection struct
|
|
var collection NML
|
|
err := xml.Unmarshal([]byte(content), &collection)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &collection, nil
|
|
}
|
|
|
|
func SaveCollectionToFile(collection *NML, filePath string) error {
|
|
// Marshal the Collection struct into XML content
|
|
content, err := xml.MarshalIndent(collection, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Write the XML content to the file
|
|
err = os.WriteFile(filePath, content, 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|