使用生成的HL7类创建纯文本元素

时间:2013-12-30 11:20:16

标签: java xml text jaxb

在旧的java 1.5 webservice中,响应消息是从包含XML的String构造的,在将Web服务迁移到java 7时,这似乎不再正常工作。 因此,我使用现有的HL7 WSDL生成类,以便创建一个java对象模型,该模型将使用Jaxb映射到XML。所以,根据旧的webservice,在acknowledgementDetail中,标签应该出现,如下所示:

<acknowledgement typeCode="CE">
        <acknowledgementDetail typeCode="E">
           <text>Some text</text>
        </acknowledgementDetail>
</acknowledgement>

但是,生成的acknowledgementDetail类的方法setText()只接受org.hl7.v3.ED类型或其子类的对象。

生成的ED类只有一个相关方法:可通过ed.getAny()

访问的元素列表(List)
ED ed = new ED();
ed.getAny().add(document.getDocumentElement());
acknowledgementDetail.setText(ed);
acknowledgement.getAcknowledgementDetail().add(acknowledgementDetail);

我创建了上面的“文档”:

DocumentBuilder documentBuilder;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

try {
     documentBuilder = documentBuilderFactory.newDocumentBuilder();
     document = documentBuilder.newDocument();                           
     Node node = document.createElement("dummy");
     node.setTextContent("Some text");                             
     document.appendChild(node);                
} catch (ParserConfigurationException e) {
    sLog.error("ParserConfigurationException: ", e);
}

对我来说,这对于一个字符串来说似乎是一项非常多的工作,但除了这一点之外......

当我创建一个org.w3c.dom.Element时,我被迫给Element一个名字(在这种情况下为“dummy”),否则Jaxb会因异常而失败。 我一直得到的结果是这样的:

<acknowledgement typeCode="CE">
    <acknowledgementDetail typeCode="E">
        <text>
            <dummy xmlns="">Some text</dummy>
        </text>
    </acknowledgementDetail>
</acknowledgement> 

在ED类的文档中,它表示子类ST应该用于文本,因此将类型ED更改为ST类型并添加:

st.setRepresentation(CsBinaryDataEncoding.TXT);

导致以下结果:

<acknowledgement typeCode="CE">
    <acknowledgementDetail typeCode="E">
        <text representation="TXT" xsi:type="ST">
            <dummy xmlns="">Some text</dummy>
        </text>
     </acknowledgementDetail>
</acknowledgement>

......这显然无法解决我的问题:S

我不明白,如何在元素中获得纯文本?!? 欢迎任何建议,这需要尽快完成并尽快工作,所以任何可能的解决方案都会受到欢迎!

更新:

AcknowledgementDetail complextype:

<xs:complexType name="MCCI_MT000200.AcknowledgementDetail">
    <xs:sequence>
    <xs:element name="code" type="CE" minOccurs="0"/>
    <xs:element name="text" type="ED" minOccurs="0"/>
    <xs:element name="location" type="ST" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="type" type="Classes" default="AcknowledgementDetail"/>
<xs:attribute name="typeCode" type="cs" use="optional"/>
<xs:attribute name="templateId" use="optional">
<xs:simpleType>
    <xs:list itemType="oid"/>
</xs:simpleType>
</xs:attribute>
    <xs:attribute name="typeID" use="optional">
<xs:simpleType>
    <xs:list itemType="oid"/>
</xs:simpleType>
</xs:attribute>
    <xs:attribute name="realmCode" use="optional">
    <xs:simpleType>
        <xs:list itemType="cs"/>
    </xs:simpleType>
</xs:attribute>
<xs:attribute name="nullFlavor" type="cs" use="optional"/>

ED complextype:

<xsd:complexType name="ED" mixed="true">
    <xsd:annotation>
        <xsd:documentation> Data that is primarily intended for human interpretation or for
            further machine processing is outside the scope of HL7. This includes unformatted or
            formatted written language, multimedia data, or structured information as defined by
            a different standard (e.g., XML-signatures.) Instead of the data itself, an ED may
            contain only a reference (see TEL.) Note that the ST data type is a specialization
            of the ED data type when the ED media type is text/plain.</xsd:documentation>
        <xsd:appinfo/>
    </xsd:annotation>
    <xsd:complexContent mixed="true">
        <xsd:extension base="BIN">
            <xsd:sequence>
                <xsd:element name="reference" type="TEL" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation> A telecommunication address (TEL), such as a URL for
                            HTTP or FTP, which will resolve to precisely the same binary data
                            that could as well have been provided as inline data.</xsd:documentation>
                        <xsd:appinfo/>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="thumbnail" type="thumbnail" minOccurs="0"/>
                <xsd:any namespace="##other" processContents="skip" minOccurs="0"
                    maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute name="mediaType" type="cs" use="optional" default="text/plain">
                <xsd:annotation>
                    <xsd:documentation> Identifies the type of the encapsulated data and
                        identifies a method to interpret or render the data.</xsd:documentation>
                    <xsd:appinfo/>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="language" type="cs" use="optional">
                <xsd:annotation>
                    <xsd:documentation> For character based information the language property
                        specifies the human language of the text.</xsd:documentation>
                    <xsd:appinfo/>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="compression" type="cs_CompressionAlgorithm" use="optional">
                <xsd:annotation>
                    <xsd:documentation> Indicates whether the raw byte data is compressed, and
                        what compression algorithm was used.</xsd:documentation>
                    <xsd:appinfo/>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="integrityCheck" type="bin" use="optional">
                <xsd:annotation>
                    <xsd:documentation> The integrity check is a short binary value representing
                        a cryptographically strong checksum that is calculated over the binary
                        data. The purpose of this property, when communicated with a reference
                        is for anyone to validate later whether the reference still resolved to
                        the same data that the reference resolved to when the encapsulated data
                        value with reference was created.</xsd:documentation>
                    <xsd:appinfo/>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="integrityCheckAlgorithm" type="cs_IntegrityCheckAlgorithm"
                use="optional" default="SHA-1">
                <xsd:annotation>
                    <xsd:documentation> Specifies the algorithm used to compute the
                        integrityCheck value.</xsd:documentation>
                    <xsd:appinfo/>
                </xsd:annotation>
            </xsd:attribute>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

1 个答案:

答案 0 :(得分:0)

我最终使用生成的acknowledgementDetail的setText方法来接受字符串而不是ED。这当然非常难看,但是在编组输出XML时似乎工作得很好,而且我再也找不到合适的解决方案了。