zoukankan      html  css  js  c++  java
  • HL7 2.6解析转XML(C#版)

    <dependency>
    <groupId>org.openehealth.ipf.boot</groupId>
    <artifactId>ipf-hl7v3-spring-boot-starter</artifactId>
    <version>3.3.0</version>
    </dependency>

    HL7 2.6解析转XML(C#版)

    项目中需要解析HL7,所以在网上找到解析代码,但错误很多,所以我修改了一下,测试好用。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Text.RegularExpressions;
     
    namespace PT.Common
    {
        /// <summary>
        /// HL7解析器
        /// </summary>
        public static class HL7ToXmlConverter
        {
            private static XmlDocument _xmlDoc;
     
            /// <summary>
            /// 把HL7信息转成XML形式
            /// 分隔顺序 ,|,~,^,&
            /// </summary>
            /// <param name="sHL7">HL7字符串</param>
            /// <returns></returns>
            public static string ConvertToXml(string sHL7)
            {
                _xmlDoc = ConvertToXmlObject(sHL7);
                return _xmlDoc.OuterXml;
            }
     
            /// <summary>
            /// 通过|分隔
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            private static string[] GetMessgeFields(string s)
            {
                return s.Split('|');
            }
     
            /// <summary>
            /// 通过^分隔
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            private static string[] GetComponents(string s)
            {
                return s.Split('^');
            }
     
            /// <summary>
            /// 通过某连接符分隔
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            private static string[] GetSubComponents(string s)
            {
                return s.Split('&');
            }
     
            /// <summary>
            /// 通过~分隔 重复
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            private static string[] GetRepetitions(string s)
            {
                return s.Split('~');
            }
     
            /// <summary>
            /// 创建XML对象
            /// </summary>
            /// <returns></returns>
            private static XmlDocument CreateXmlDoc()
            {
                XmlDocument output = new XmlDocument();
                XmlElement rootNode = output.CreateElement("HL7Message");
                output.AppendChild(rootNode);
                return output;
            }
             
            /// <summary>
            /// 读取XML某节点值
            /// </summary>
            /// <param name="xmlObject"></param>
            /// <param name="path"></param>
            /// <returns></returns>
            public static string GetText(XmlDocument xmlObject, string path)
            {
                XmlNode node = xmlObject.DocumentElement.SelectSingleNode(path);
                if (node != null)
                {
                    return node.InnerText;
                }
                else
                {
                    return null;
                }
            }
     
            /// <summary>
            /// 读取XML某节点组的第index项
            /// </summary>
            /// <param name="xmlObject"></param>
            /// <param name="path"></param>
            /// <param name="index"></param>
            /// <returns></returns>
            public static string GetText(XmlDocument xmlObject, string path, int index)
            {
                XmlNodeList nodes = xmlObject.DocumentElement.SelectNodes(path);
                if (index <= nodes.Count)
                {
                    return nodes[index].InnerText;
                }
                else
                {
                    return null;
                }
            }
     
            /// <summary>
            /// 读取XML某节点组
            /// </summary>
            /// <param name="xmlObject"></param>
            /// <param name="path"></param>
            /// <returns></returns>
            public static String[] GetTexts(XmlDocument xmlObject, string path)
            {
                XmlNodeList nodes = xmlObject.DocumentElement.SelectNodes(path);
                String[] arr = new String[nodes.Count];
                int index = 0;
                foreach (XmlNode node in nodes)
                {
                    arr[index++] = node.InnerText;
                }
                return arr;
            }
     
            /// <summary>
            /// HL7字符串转XML
            /// </summary>
            /// <param name="sHL7"></param>
            /// <returns></returns>
            public static XmlDocument ConvertToXmlObject(string sHL7)
            {
                _xmlDoc = CreateXmlDoc();
     
                //把HL7分成段
                string[] sHL7Lines = sHL7.Split(' ');//经过测试,TCP方式接收的用 分隔,webService方式接收的用 分隔
                 
                //过滤一些字符
                //for (int i = 0; i < sHL7Lines.Length; i++)
                //{
                //    sHL7Lines[i] = Regex.Replace(sHL7Lines[i], @"[^ -~]", "");
                //}
     
                //遍历每一段
                for (int i = 0; i < sHL7Lines.Length; i++)
                {
                    // 判断是否空行
                    if (sHL7Lines[i] != string.Empty)
                    {
                        string sHL7Line = sHL7Lines[i];//某一段
     
                        //通过“|”分隔
                        string[] sFields = HL7ToXmlConverter.GetMessgeFields(sHL7Line);
     
                        // 为段(一行)创建第一级节点
                        XmlElement el = _xmlDoc.CreateElement(sFields[0]);
                        _xmlDoc.DocumentElement.AppendChild(el);
     
                        //遍历每个“|”与“|”间的内容
                        for (int a = 0; a < sFields.Length; a++)
                        {
                            // 为字段创建第二级节点
                            XmlElement fieldEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString());
     
                            //是否包括HL7的连接符
                            if (sFields[a] != @"^~&")//0:如果这一行有任何分隔符,继续分隔。如果没有任何分隔符,可以直接写节点值。
                            {
                                //通过"~"分隔
                                string[] sComponents = HL7ToXmlConverter.GetRepetitions(sFields[a]);
                                if (1==1)//1:如果可以用“~”分隔,继续分隔;如果没有“~”符,开始用“~”分隔。不管有没有"~"符,都循环分隔
                                {
                                    for (int b = 0; b < sComponents.Length; b++)
                                    {
                                        XmlElement componentEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString());
     
                                        //通过"^"分隔
                                        string[] subComponents = GetComponents(sComponents[b]);
                                        if (subComponents.Length > 1)//2.如果有字组,大部分是没有的
                                        {
                                            for (int c = 0; c < subComponents.Length; c++)
                                            {
                                                //修改了一个错误
                                                string[] subComponentRepetitions = GetSubComponents(subComponents[c]);
                                                if (subComponentRepetitions.Length > 1)
                                                {
                                                    for (int d = 0; d < subComponentRepetitions.Length; d++)
                                                    {
                                                        XmlElement subComponentRepEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString() + "." + c.ToString() + "." + d.ToString());
                                                        subComponentRepEl.InnerText = subComponentRepetitions[d];
                                                        componentEl.AppendChild(subComponentRepEl);
                                                    }
                                                }
                                                else
                                                {
                                                    XmlElement subComponentEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString() + "." + c.ToString());
                                                    subComponentEl.InnerText = subComponents[c];
                                                    componentEl.AppendChild(subComponentEl);
     
                                                }
                                            }
                                            fieldEl.AppendChild(componentEl);
                                        }
                                        else    //2.如果没有字组了,大部分是没有
                                        {
                                            string[] sRepetitions = HL7ToXmlConverter.GetSubComponents(sComponents[b]);
                                            if (sRepetitions.Length > 1)
                                            {
                                                XmlElement repetitionEl = null;
                                                for (int c = 0; c < sRepetitions.Length; c++)
                                                {
                                                    repetitionEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString() + "." + c.ToString());
                                                    repetitionEl.InnerText = sRepetitions[c];
                                                    componentEl.AppendChild(repetitionEl);
                                                }
                                                fieldEl.AppendChild(componentEl);
                                                el.AppendChild(fieldEl);
                                            }
                                            else
                                            {
                                                componentEl.InnerText = sComponents[b];
                                                fieldEl.AppendChild(componentEl);
                                                el.AppendChild(fieldEl);
                                            }
                                        }
                                    }
                                    el.AppendChild(fieldEl);
                                }             
                            }
                            else
                            {
                                //0:如果不可以分隔,可以直接写节点值了。
                                fieldEl.InnerText = sFields[a];
                                el.AppendChild(fieldEl);
                            }
                        }
                    }
                }
     
     
     
                return _xmlDoc;
            }
     
             
             
            //测试方法转XML
            //string sHL7asXml = PT.Common.HL7ToXmlConverter.ConvertToXml(hl7Data);
            //ThreadUPtextBoxMsg(textBoxMsgAppendText, " [解析HL7消息]" + sHL7asXml);
            XmlDocument xmlObject = PT.Common.HL7ToXmlConverter.ConvertToXmlObject(hl7Data);
            String chuang1 = PT.Common.HL7ToXmlConverter.GetText(xmlObject, "PV1/PV1.6/PV1.6.0/PV1.6.0.2", 0);
            String chuang2 = PT.Common.HL7ToXmlConverter.GetText(xmlObject, "PV1/PV1.3/PV1.3.0/PV1.3.0.2", 0);
            ThreadUPtextBoxMsg(textBoxMsgAppendText, " [解析HL7消息为XML]" + name + "从" + chuang1 + "床换到" + chuang2 + "床");
             
        }
     
    }

      

    解析后的XML,可读性比较差。

    C#格式化XML的代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    /// <summary>
    /// 格式化XML(C#版)
    /// </summary>
    /// <param name="sUnformattedXml"></param>
    /// <returns></returns>
    public static string FormatXml(string sUnformattedXml)
    {
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(sUnformattedXml);
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        XmlTextWriter xtw = null;
        try
        {
            xtw = new XmlTextWriter(sw);
            xtw.Formatting = Formatting.Indented;
            xtw.Indentation = 1;
            xtw.IndentChar = ' ';
            xd.WriteTo(xtw);
        }
        finally
        {
            if (xtw != null)
                xtw.Close();
        }
        return sb.ToString();
    }
  • 相关阅读:
    Android 开发工具类 19_NetworkStateReceiver
    Android 开发工具类 18_NetWorkUtil
    Sticky Footer (让页脚永远停靠在页面底部,而不是根据绝对位置)
    min-height最小高度的实现(兼容IE6、IE7、FF)(解决IE6不兼容min-height)
    不同浏览器设置背景透明度
    讨论内外边距对行内元素是否起作用,则要对行内替换元素和行内非替换元素分别讨论:
    超链接访问过后hover样式就不出现的问题
    解决:子元素设置margin-top,父元素也受影响的问题
    制作0.5px像素的细条
    去掉inline-block元素间隙的几种方法
  • 原文地址:https://www.cnblogs.com/zhoading/p/12123936.html
Copyright © 2011-2022 走看看