FHIR Chat · Source code...Generate contained xml from Java HAPI Bundle? · hapi

Stream: hapi

Topic: Source code...Generate contained xml from Java HAPI Bundle?


view this post on Zulip Jingdong(JD) Li (Sep 17 2016 at 11:59):

I have a bundle resource structured like: bundle/entry/resourece/DiagnosticReport/contained/List/contained/Observation, using Java FHIR HAPI, somehow, the contained portions were not convered to XML using FHIR context parser. Cannot figure out what I did wrong. Thanks in advance.

Below is my testing code (in Java)
===========================================================
public class FHIRContaintedTest {
private Logger logger;
public FHIRContaintedTest(){
System.setProperty("log4j.configurationFile", FHIRContaintedTest.class.getClassLoader().getResource("log4j2.xml").toString());
logger = LogManager.getLogger(FHIRContaintedTest.class.getName());
}
public static void main(String[] args) {
FhirContext ctx = FhirContext.forDstu2();
IParser parser = ctx.newXmlParser().setPrettyPrint(true);
FHIRMaker fm = new FHIRMaker();
FHIRContaintedTest fc = new FHIRContaintedTest();
Bundle bundle = fm.makeBundle("JD-Bundle");
bundle.setType(BundleTypeEnum.COLLECTION);
Entry rootEntry = fm.makeBundleEntry("JD-Entry");
DiagnosticReport dxReport = fm.makeDiagnosticReport("JD-Drpt");
ListResource lstRsc = fm.makeListRsc("JD-ListRsc");
Observation obs = fm.makeObservation("JD-Obs");
//add Observation to ListResource.contained
fc.addToContained(obs, lstRsc);
//add ListResource to DiagnosticReport.contained
fc.addToContained(lstRsc, dxReport);
rootEntry.setResource(dxReport);
bundle.addEntry(rootEntry);
String encodeStr = parser.encodeResourceToString(bundle);
fc.logger.trace(encodeStr);
}

public void addToContained(BaseResource sml, BaseResource large){
ContainedDt contained = new ContainedDt();
ArrayList arrLst = new ArrayList<>();
arrLst.add(sml);
contained.setContainedResources(arrLst);
large.getContained().setContainedResources(arrLst);
}
}
=======================================================================
public class FHIRMaker {
public FHIRMaker() {
// TODO Auto-generated constructor stub
}
// Make FHIR Observation
public Observation makeObservation(String obsName) {
Observation theObs = new Observation(); //
theObs.setId("1");
theObs.setStatus(ObservationStatusEnum.FINAL);
theObs.getCode().setText(obsName);
StringDt val = new StringDt();
val.setValue(obsName + "-value");
theObs.setValue(val);
return theObs;
}
// Make DiagnosticReport
public DiagnosticReport makeDiagnosticReport(String drptName) {
DiagnosticReport drpt = new DiagnosticReport();
drpt.setId("1");
drpt.getCode().setText("test report");
ResourceReferenceDt pfm = new ResourceReferenceDt();
pfm.getReference().setValue("Lab Center");
drpt.setPerformer(pfm);
return drpt;
}
// Make Bundle
public Bundle makeBundle(String bdlName) {
Bundle bdl = new Bundle();
bdl.setType(BundleTypeEnum.DOCUMENT);
return bdl;
}
// Make ListResoruces
public ListResource makeListRsc(String lstName) {
ListResource lstRsc = new ListResource();
lstRsc.setId("1");
return lstRsc;
}
// Make Bundle-Entry
public Entry makeBundleEntry(String entName) {
Entry ent = new Entry();
return ent;
}
}
======Below is the XML output, all contained XML elements are missing======================
<Bundle xmlns="http://hl7.org/fhir">
<type value="collection"></type>
<entry>
<resource>
<DiagnosticReport xmlns="http://hl7.org/fhir">
<id value="1"></id>
<code>
<text value="test report"></text>
</code>
<performer>
<reference value="Lab center"></reference>
</performer>
</DiagnosticReport>
</resource>
</entry>
</Bundle>

view this post on Zulip Jingdong(JD) Li (Sep 17 2016 at 14:07):

ok,have the problem solved. It looks like for any reason, if you defined an internal observation or other reousrce, the resource contained the internal resource has to particularly reference it, otherwise, FHIR parser will ignore the contained resource during xml generation.


Last updated: Apr 12 2022 at 19:14 UTC