【问题标题】:printing HL7 message in console在控制台中打印 HL7 消息
【发布时间】:2011-11-24 08:19:42
【问题描述】:

我将一个对象传递给构造函数,然后将此对象的参数添加到 HL7。 ORU_R01 是 HL7 的类型。 当我将 HL7 打印到控制台时,只打印最后一个 OBX。 我的代码有什么问题? 如何将此 HL7 消息写入套接字? java中有没有更简单的方法来处理HL7?

public class FlexSMessageHL7 {
private FileWriter writeHL7ToFile;
private PrismaflexSMessage sMessage;
private ORU_R01 message;
private int i = 0;
private OBX obx = null;

public FlexSMessageHL7(FlexSMessage sMessage) {
    this.sMessage = sMessage;
    this.message = new ORU_R01();
    createHL7SMessage();
}

public void createHL7SMessage() {

    // Populate the MSH Segment
    MSH msh = message.getMSH();
    try {
        msh.getFieldSeparator().setValue("|");
        msh.getEncodingCharacters().setValue("^~\\&");
        msh.getDateTimeOfMessage().setValue(sMessage.getTime().toString());
        msh.getSendingApplication().getNamespaceID().setValue(String.valueOf(sMessage.getMachID()));
    } catch (DataTypeException e) {
        e.printStackTrace();
    }

    // Populate the OBR Segment:time
    OBR obr = message.getPATIENT_RESULT().getORDER_OBSERVATION().getOBR();
    try {
        obr.getObservationDateTime().setValue(String.valueOf(sMessage.getTime()));
    } catch (DataTypeException e) {
        e.printStackTrace();
    }

    // Populate the PID Segment:PatientId
    PID pid = message.getPATIENT_RESULT().getPATIENT().getPID();
    try {
        pid.getPatientID().getIDNumber().setValue(sMessage.getPatID());
    } catch (HL7Exception e) {
        e.printStackTrace();
    }

    // Populate the OBX Segment:Param_Code, time, Measure_Value
    while (i < sMessage.getMsgInfo()) {
        for (PrismaflexSRecord sRecord : sMessage.getsRecordCollection()) {
            try {
                obx = message.getPATIENT_RESULT().getORDER_OBSERVATION().getOBSERVATION(i).getOBX();
                obx.getSetIDOBX().setValue(String.valueOf(i));
                obx.getObservationIdentifier().getIdentifier().setValue(sRecord.getParamCode());
                obx.getDateTimeOfTheObservation().setValue(String.valueOf(sRecord.getTimeStamp()));
                obx.getObservationIdentifier().getNameOfCodingSystem().setValue(String.valueOf(sRecord.getMeasureValue()));
                i++;
            } catch (HL7Exception e) {
                e.printStackTrace();
            }
        }

    }
    try {
        writeHL7ToFile = new FileWriter(File.createTempFile("prismaflexOutputFrom3001HL7", "txt", new File
                ("c:\\tmp\\prismaflex")));
        writeHL7ToFile.write(message.getMSH().toString());
        writeHL7ToFile.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Now, Encode the message and look at the output
    try {

        Parser parser = new PipeParser();
        String encodedMessage = parser.encode(message);
        System.out.println("Printing HL7 Encoded Message:");
        System.out.println(encodedMessage);
    } catch (HL7Exception e) {
        e.printStackTrace();
    }
}

}

【问题讨论】:

  • 将 \r 替换为 \n。它真正做的是在同一控制台行中打印所有段,因此您将看到最后一段,但如果最后一段比前一段短,您将在最后看到前一段的字段。尝试将消息写入文件,您将在其中看到所有段。

标签: java socket.io hl7


【解决方案1】:

您是否考虑过使用 HAPI?它是为 java 编写的,它的对应部分 nHAPI 也是为 .net 编写的。详情在这里:

http://hl7api.sourceforge.net/

【讨论】:

    【解决方案2】:

    正如 Nicholas Orlowski 指出的那样,问题在于行尾字符,根据 HL7 标准,CR 字符使 Windows 命令提示符仅将光标重置到行首并用下一行的内容覆盖它.因此,对于控制台输出,您需要将行尾替换为其他内容。

    对于最近一个使用 HAPI 的 HL7 应用,你似乎也在使用,我做了一个小助手方法来实现这个功能:

    private static String replaceNewlines(String input) {
        return input.replaceAll("\\r", "\n");
    }
    

    该函数可以在所有平台上使用,因为它将 CR 字符替换为特定于操作系统的换行符。

    然后我可以使用它来输出到控制台,如下所示:

    LOGGER.trace("Generated message contents:\n" + replaceNewlines(outMessage.encode()));
    

    在这种情况下,我使用 log4j 记录到控制台,而不是简单的控制台打印输出,但问题对我来说是一样的。

    希望对你有帮助!

    【讨论】:

      【解决方案3】:

      我在我的 python HL7py 库中遇到了类似的问题。很多时候控制台不喜欢打印字符。我必须编写一个帮助程序,将 CR 更改为 LF(换行)以正确显示行。希望对您有所帮助。

      【讨论】:

        【解决方案4】:

        它不会显示在控制台中,但会在您写入文件时显示。尝试在调试模式下查看变量并将其写入文件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多