I need to extract a few fields from an XML file and wonder how to go about it.
First, some context:
I need to fetch a libvirt VM’s snapshot information, which is encoded in XML.
I do this with data, _ := snap.GetXMLDesc(0)
(data is a string var… so yes, libvirt returns ±8kb files in a single string var).
If I write that string in an xml file, it amounts to 188 lines (7.8kb) of fileds + attributes. I only need 3 of them, really.
From various readings on the net, I gather that to unmarshall the XML file, I would need to create a data struct where I’d map all fields/attributes, and so on. Surely there’s a better way, when I need about 3 lines of that XML file?
One workaround I thought about is to dump that string var in a file, and “grep” within that file to get my info, but found that un-elegant. There must be a way to only map the info I need from that file than that ?
Here’s a sample of the XML file. Let’s say I wanted only the “parent”, “creation type” and “type arch” fileds + attributes, besides dumping the XML to a file, I do not see my way around that.
Easier yet than XPath, @clbanning , thanks. I did not think you could “partial map” between a struct and an xml doc. I was to test it in the playground, and well, got carried away with @nobbz’s solution.
Ok, @clbanning , I thought it worked, but it does not (note to self: compiles != works). I’m not sure if it’s my limited so-far knowledge of GO, or my rusty rememberance of XML docs: here’s an edited version of the XML I need to parse: