XML文件和有效载荷

时间:2015-12-22 10:35:04

标签: xml mule

我有这个XML文件:

<phone>
<brand>LG</brand>
<price>10</price>
<screenSize>8</screenSize>
</phone>

My Flow

如何通过文件连接器转换收到的XML文件以用作#[payload]?

2 个答案:

答案 0 :(得分:4)

文件连接器 无法转换有效负载。
它只是从文件中获取有效负载并将其发送到流程中 您需要做的是从XML中提取元素以执行数据库访问,例如插入,更新,选择等。<登记/> 要提取元素值,您需要在答案中使用@JoostD已经提到的 XPATH3 参考: - https://docs.mulesoft.com/mule-user-guide/v/3.7/xpath

通过简单示例扩展@JoostD回答更清楚,如下所述: -
在这里,您 XML 文件中提取值并插入数据库表中: -

  <file:connector name="File_Input" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
    <db:generic-config name="Generic_Database_Configuration" doc:name="Generic Database Configuration"/>
     <flow name="testxmlFlow1" > 
        <file:inbound-endpoint path="C:\Users\u450146\Downloads" responseTimeout="10000" doc:name="File"  connector-ref="File_Input">
            <file:filename-regex-filter pattern="Test.xml" caseSensitive="false"/>
        </file:inbound-endpoint>  
        <byte-array-to-string-transformer doc:name="Byte-Array-to-String" />  
        <splitter expression="#[xpath3('/phone', payload, 'NODESET')]" doc:name="Splitter" />
        <logger message="brand :- #[xpath3('brand')]  price:- #[xpath3('price')] screenSize:-  #[xpath3('screenSize')]" level="INFO" doc:name="Logger"/>
       <!-- Your Database code here -->
        <db:insert config-ref="Generic_Database_Configuration" doc:name="Database">
            <db:parameterized-query><![CDATA[INSERT INTO table1(brand,price,screenSize)VALUES (#[xpath3('brand')],#[xpath3('price')],#[xpath3('screenSize')])]]></db:parameterized-query>
        </db:insert>  
    </flow >​

现在您可以看到,文件中的文件入站选择了有效负载,然后变换器转换成为字符串,然后 Splitter 用于拆分值: -

 <splitter expression="#[xpath3('/phone', payload, 'NODESET')]" doc:name="Splitter" />

现在,您可以使用 XPATH3 直接将XML元素值插入数据库,并使用以下SQL查询: -

INSERT INTO table1(brand,price,screenSize)VALUES (#[xpath3('brand')],#[xpath3('price')],#[xpath3('screenSize')])​

希望获得此帮助,您可以使用提取的数据执行其他 SQL查询,如上所示:)

答案 1 :(得分:0)

使用File to String变换器。

https://docs.mulesoft.com/mule-user-guide/v/3.7/file-transport-reference#sts=File to String Transformer

之后,您可以使用Xpath从XML元素中获取值。

https://docs.mulesoft.com/mule-user-guide/v/3.7/xpath#sts=XPath

例如:#[xpath('/ phone / brand')]

相关问题