将HL7 v2转换为JSON [英] Converting HL7 v2 to JSON
问题描述
我希望将HL7 v2(旧的EDI格式)消息转换为JSON,因此我可以使其在Apache Drill下可处理,在Parquet下可压缩.
I am looking to convert HL7 v2 (older EDI format) messages to JSON, so I could make them processable under Apache Drill and compressible under Parquet.
我调查了 HAPI ,但是我没有运气非XML HL7到JSON的转换.
I looked into HAPI, but I am not having luck finding utility for non-XML HL7 to JSON conversion.
有人对图书馆有建议或参考吗?
Does anyone have a suggestion or a reference to a library?
推荐答案
只需使用HAPI即可转换为XML.下面的代码需要Saxon,因为XML到JSON需要XSLT 2.0,但是如果您已经有一种将XML转换为JSON的方法,则只需要前两行,它们完全是HAPI.当然,您应该在本地下载XSLT以进行生产. :-)
Just use HAPI to convert to XML. The code below requires Saxon, because the XML-to-JSON requires XSLT 2.0, but if you already have a method to convert XML to JSON, then you just need the first two lines, which are entirely HAPI. You should download the XSLT locally for production, of course. :-)
String convertHL7ToJson(Message message) {
try {
DefaultXMLParser xmlParser = new DefaultXMLParser(new CanonicalModelClassFactory("2.6"));
String xml = xmlParser.encode(message);
Transformer xmlTransformer = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null).newTransformer(
new StreamSource(new StringReader(readFileFromURL("https://github.com/bramstein/xsltjson/raw/master/conf/xml-to-json.xsl")))
);
StringWriter result = new StringWriter();
xmlTransformer.transform(
new StreamSource(new StringReader(xml)), new StreamResult(result)
);
return result.toString();
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
String readFileFromURL(String url) {
InputStream is = null;
try {
return new Scanner(is = new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next();
} catch (Throwable t) {
t.printStackTrace();
} finally {
if(is != null)
try {
is.close();
} catch (Throwable ignored){}
}
return null;
}
这将创建如下输出:
"ORM_O01":{"MSH":{"MSH.1":"|","MSH.2":"^~\\&","MSH.3":{"HD.1":"TEST"},"MSH.4":{"HD.1":"TEST000","HD.2":"BL"},...
这篇关于将HL7 v2转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!