Skip to content

Instantly share code, notes, and snippets.

@eedrummer
Created January 25, 2016 21:53
Show Gist options
  • Save eedrummer/5b4f2bfbace224d7478f to your computer and use it in GitHub Desktop.
Save eedrummer/5b4f2bfbace224d7478f to your computer and use it in GitHub Desktop.
FHIR XML transform to make more golang friendly
<Bundle xmlns="http://hl7.org/fhir">
<id value="10bb101f-a121-4264-a920-67be9cb82c74"/>
<type value="message"/>
<entry>
<fullUrl value="urn:uuid:267b18ce-3d37-4581-9baa-6fada338038b"/>
<resource>
<MessageHeader>
<id value="efdd254b-0e09-4164-883e-35cf3871715f"/>
<timestamp value="2015-12-08T11:15:33-05:00"/>
<event>
<system value="https://github.com/pophealth/fhir/message-events"/>
<code value="record-match"/>
</event>
<source>
<endpoint value="https://acme.com/pophealth"/>
</source>
<destination>
<endpoint value="http://acme.com/record-matcher"/>
</destination>
<author>
<endpoint value="urn:uuid:15121321-4af5-424c-a0e1-ed3aab1c350a"/>
</author>
<data>
<reference value="urn:uuid:15121321-4af5-424c-a0e1-ed3aab1c348e"/>
</data>
</MessageHeader>
</resource>
</entry>
<entry>
<fullUrl value="urn:uuid:15121321-4af5-424c-a0e1-ed3aab1c348e"/>
<resource>
<Parameters>
<parameter>
<name value="type"/>
<valueString value="master"/>
</parameter>
<parameter>
<name value="resourceType"/>
<valueString value="Patient"/>
</parameter>
<parameter>
<name value="searchExpression" />
<resource>
<Parameters>
<parameter>
<name value="resourceUrl"/>
<valueUri value="http://acme.com/popHealth/fhir/Patient"/>
</parameter>
<parameter>
<name value="name"/>
<valueString value="jon"/>
</parameter>
</Parameters>
</resource>
</parameter>
</Parameters>
</resource>
</entry>
<entry>
<fullUrl value="urn:uuid:15121321-4af5-424c-a0e1-ed3aab1c350a"/>
<resource>
<Practitioner>
<identifier>
<use value="usual"/>
<system value="https://github.com/pophealth/users" />
<value value="user1" />
</identifier>
</Practitioner>
</resource>
</entry>
</Bundle>
package main
import (
"bytes"
"encoding/xml"
"os"
)
func main() {
var b bytes.Buffer
bundle, err := os.Open("bundle.xml")
if err != nil {
panic(err.Error())
}
decoder := xml.NewDecoder(bundle)
encoder := xml.NewEncoder(&b)
for {
token, _ := decoder.Token()
//fmt.Println(token)
if token == nil {
break
}
switch startElement := token.(type) {
case xml.StartElement:
fhirValue := findValue(startElement.Attr)
if fhirValue != "" {
encoder.EncodeElement(fhirValue, xml.StartElement{Name: startElement.Name})
decoder.Skip()
} else {
encoder.EncodeToken(startElement)
}
default:
encoder.EncodeToken(token)
}
}
encoder.Flush()
b.WriteTo(os.Stdout)
}
func findValue(attr []xml.Attr) string {
for _, a := range attr {
if a.Name.Local == "value" {
return a.Value
}
}
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment