Sidebar

How do I add namespacing to an xml output message

0 votes
301 views
asked Oct 14, 2020 by scott-b-8720 (560 points)

Is there a best practice for adding namespacing to xml output?  I effectively need my output to look like this:

<ns0:Visit xmlns:ns0="http://my.url">
  <ns0:PatientFirstName>First</ns0:PatientFirstName>
  <ns0:PatientLastName>Last</ns0:PatientLastName>
  <ns0:InsurancePlans>
    <ns0:InsurancePlan>
      <ns0:PlanOrder>1</ns0:PlanOrder>
      <ns0:Name>Medicaid</ns0:Name>
    </ns0:InsurancePlan>
    <ns0:InsurancePlan>
      <ns0:PlanOrder>2</ns0:PlanOrder>
      <ns0:Name>BlueCross</ns0:Name>
    </ns0:InsurancePlan>
  </ns0:InsurancePlans>
</ns0:Visit>

...but when i attempt to add child elements using setNode and include the "ns0:" I get errors.  I've found that when I create my xml message I can give it a template that includes the namespacing, but in doing that i don't always know how many InsurancePlan elements I'll need to output--as such, my template only specs out one.  when I try to do a message.addChild I run into issues again with setNode.

Here is my template:

<ns0:Visit xmlns:ns0="http://my.url">
    <ns0:PatientFirstName></ns0:PatientFirstName>
    <ns0:PatientLastName></ns0:PatientLastName>
    <ns0:InsurancePlans>
        <ns0:InsurancePlan>
            <ns0:PlanOrder></ns0:PlanOrder>
            <ns0:Name></ns0:Name>
        </ns0:InsurancePlan>
    </ns0:InsurancePlans>
</ns0:Visit>

...and here is some brief sample code

message = qie.createXMLMessage(qie.getVariable('XmlTemplate'), "UTF-16");
message.setNode("/Visit/PatientFirstName", source.getNode("/patient/firstName"));
message.setNode("/Visit/PatientLastName", source.getNode("/patient/lastName"));
message.setNode("/Visit/InsurancePlans", "InsurancePlan", 1, true);
var insCount = source.getCount("/insurance");
var insObject = qie.parseJSONString(source.getNode("/insurance"));
for (var insCounter = 1; insCounter <= insCount; insCounter++) {
   var insRoot = "/[" + insCounter + "]";
   var insuranceName = insObject.getNode(insRoot + "/name");
   if (StringUtils.length(insuranceName) > 0) {
        message.addChild("/Visit/InsurancePlans", "InsurancePlan", "");
        var msgInsRoot = "/Visit/InsurancePlans/InsurancePlan[" + insCounter.toString() + "]";
        if (insCounter === 1)
        {
          msgInsRoot = "/Visit/InsurancePlans/InsurancePlan";
        }
       message.setNode(msgInsRoot + "/PlanOrder", insObject.getNode(insRoot + "/position"));
       message.setNode(msgInsRoot + "/Name", insuranceName);
   }
}

When I do it that way my second InsurancePlan values end up without the namespacing (though my first ones get it) 

commented Oct 15, 2020 by scott-b-8720 (560 points)
The answer below worked.  Thank you!

1 Answer

0 votes
 
Best answer

If you addChild before you set the value you can include the namepsace.

 

message.setNode("/Visit/PatientFirstName", source.getNode("/patient/firstName"));
message.setNode("/Visit/PatientLastName", source.getNode("/patient/lastName"));
message.setNode("/Visit/InsurancePlans", "InsurancePlan", 1, true);
var insCount = source.getCount("/insurance");
var insObject = qie.parseJSONString(source.getNode("/insurance"));

for (var insCounter = 1; insCounter <= insCount; insCounter++) {
   var insRoot = "/[" + insCounter + "]";
   var insuranceName = insObject.getNode(insRoot + "/name");
   var position = insObject.getNode(insRoot + "/position");
   if (StringUtils.isNotBlank(insuranceName)) {
      message.addChild("/Visit/InsurancePlans", "ns0:insurance");
      
      message.addChild("/Visit/InsurancePlans/insurance["+insCounter+"]", "ns0:Name");
      message.setNode("/Visit/InsurancePlans/insurance["+insCounter+"]/Name", insuranceName);
      
      message.addChild("/Visit/InsurancePlans/insurance["+insCounter+"]", "ns0:position");
      message.setNode("/Visit/InsurancePlans/insurance["+insCounter+"]/position", position);
   }
}

answered Oct 15, 2020 by michael-h-5027 (14,350 points)
selected Oct 15, 2020 by scott-b-8720
...