web stats
Java Script Writer - can not change XML fields values - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 02-22-2016, 06:08 AM
reymed reymed is offline
Mirth Newb
 
Join Date: Nov 2015
Posts: 6
reymed is on a distinguished road
Default [SOLVED] Java Script Writer - can not change XML fields values

Hi,

I am fighting with JS writer. I need to manipulate values of some fields of XML i.e. I want to have XML nearly 1:1, I need to swap some fields only. In JS Writer I have something like this:
Code:
var xmlmsg = new XML(connectorMessage.getRawData());
logger.info(xmlmsg['id']['@root'].toString());
xmlmsg['id']['@root'] = 'something';
var filename = 'test.xml';
var directory = 'C:\\\\test';
FileToWrite = java.io.File(directory+'\\\\'+filename);
org.apache.commons.io.FileUtils.writeStringToFile(FileToWrite, xmlmsg, 'utf-8', false);
Result of logger.info(xmlmsg['id']['@root'].toString()); is always empty although root has value in inbound XML. Moreoever there is no way to alter root value in outbound XML.
I did try with xmlmsg.id.root = 'something' but it also does not work.
It looks like mirth is not treating xmlmsg like XML.
can you help me with this please?

PS I would like to avoid using templates.
This is the begining of incoming XML:
Code:
<AnnotatedECG xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3/HL7/aECG/2003-12/schema/PORT_MT020001.xsd">
	<id root="044f0ed0-01e0-87a0-01be-329000b532b8"/>
	<code code="93000" codeSystem="2.16.840.1.113883.6.12"/>
	<effectiveTime>
		<low value="20160215172818"/>
		<high value="20160215172828"/>
	</effectiveTime>

SOLVED!

JavaScript Writer needs Namespace to access fields (see this).

I modified my script:
Code:
var outmsg = new XML(connectorMessage.getRawData().toString());
var xmlmsg = new XML(outmsg.toXMLString());
var ns = new Namespace("urn:hl7-org:v3")
default xml namespace=ns
logger.info(outmsg['effectiveTime']['low']['@value'].toString());
xmlmsg['effectiveTime']['low']['@value'] = "123456789";


var fn = $('inFileName').toString();
var arr = fn.split('_');
var pid = arr[0];
if (pid == null) {pid = "000000"; }
var filename = fn;
var directory = 'C:\\\\test';
FileToWrite = java.io.File(directory+'\\\\'+filename);
org.apache.commons.io.FileUtils.writeStringToFile(FileToWrite, xmlmsg, 'utf-8', false);
Now I can access fields - read them and write to them

Last edited by reymed; 03-04-2016 at 02:18 AM.
Reply With Quote
Reply

Tags
java script writer, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 10:52 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation