简体   繁体   English

在Mule中使用XML Transformer

[英]Using XML Transformer in Mule

I currently receive a string message from a queue, The string message contains XML. 我当前从队列收到一个字符串消息,该字符串消息包含XML。 How do I pick each element and convert to a POJO. 如何选择每个元素并转换为POJO。 This is the content of my queue 这是我队列的内容

<MsgContent>
   <MsgHeader>
       <code>1010</code>
   </MsgHeader>
   <MsgBody>
        <value>fundstransfer</value>
   </MsgBody>
</MsgContent>

I want to be able to get the values of the two keys(code and value) 我希望能够获得两个键的值(代码和值)

This is my mule configuration xml 这是我的mule配置xml

    <?xml version="1.0" encoding="UTF-8"?>

    <mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:data-        mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"          xmlns:xm="http://www.mulesoft.org/schema/mule/xml"  xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file"   xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-current.xsd
    http://www.mulesoft.org/schema/mule/xml  http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
    http://www.mulesoft.org/schema/mule/core  http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
    http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
   <jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>

<mulexml:jaxb-context name="myJaxb" doc:name="myJaxb" packageNames="com.test.jaxb" />
<flow name="JMSMessageFlow1" doc:name="JMSMessageFlow1">
    <jms:inbound-endpoint queue="StudioIns" connector-ref="Active_MQ" doc:name="JMS" />
    <custom-transformer class="org.mule.module.xml.transformer.XmlToXMLStreamReader" />
    <mulexml:jaxb-xml-to-object-transformer  jaxbContext-ref="myJaxb" returnClass="com.test.jaxb.MsgContent"/>
    <component class="com.test.HelloMessage" doc:name="Java"/>
    <file:outbound-endpoint path="D:\Documents\MuleStudio\workspace\jmsmessage\src\main\java\com\test" outputPattern="output.csv" responseTimeout="10000" doc:name="File"/>
</flow>

But still can't retrieve the objects 但仍然无法检索对象

You can't use the xml namespace for this, it's reserved. 你不能使用xml命名空间,它是保留的。 People typically use xm or mulexml . 人们通常使用xmmulexml

For example: 例如:

xmlns:xm="http://www.mulesoft.org/schema/mule/xml"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd"

...

<xm:xml-to-object-transformer />

In my app i have a xml declaring all the transformers i need to use so that i just reference them on my flows. 在我的应用程序中,我有一个xml声明我需要使用的所有变换器,以便我只是在我的流程上引用它们。 See below. 见下文。 (You dont need to use iso-8859-1). (你不需要使用iso-8859-1)。 This one considers you intend to use JAXB to bind the XML to POJO. 这个认为您打算使用JAXB将XML绑定到POJO。

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mxml="http://www.mulesoft.org/schema/mule/xml"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:spring="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/3.2/mule-xml.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.1/mule-vm.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<mxml:jaxb-context
name="jaxb-context"
packageNames="my.package.v1:my.package.v2:soap.package" />

<mxml:jaxb-xml-to-object-transformer
name="xml-to-object"
encoding="ISO-8859-1"
jaxbContext-ref="jaxb-context"
ignoreBadInput="true" />

<mxml:jaxb-object-to-xml-transformer
name="object-to-xml"
encoding="ISO-8859-1"
jaxbContext-ref="jaxb-context"
returnClass="java.lang.String"
ignoreBadInput="true" />

<byte-array-to-string-transformer
name="byte-array-to-string"
encoding="ISO-8859-1"
returnClass="java.lang.String" />

<string-to-byte-array-transformer
name="string-to-byte-array"
encoding="ISO-8859-1"
returnClass="byte[]" />

<custom-transformer
name="xml-to-xml-stream-reader"
class="org.mule.module.xml.transformer.XmlToXMLStreamReader" />

<object-to-string-transformer name="object-to-string" />

<flow name="ByteArrayToObjectXml">

<vm:inbound-endpoint
    path="myapp/conversor/byte-array-to-object-xml"
    exchange-pattern="request-response" />

<transformer ref="byte-array-to-string" />
<transformer ref="xml-to-object" />
</flow>

I have resolved it now. 我现在已经解决了。 The major issue is that one must have a valid and correct JAXB Binding Class. 主要问题是必须有一个有效且正确的JAXB绑定类。 Thanks all 谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM