使用mulesoft

时间:2016-07-29 07:11:43

标签: file parsing mule

我正在尝试解析从磁盘读取的Simple XMl文件并将其转换为JSON并使用Mulesoft将其存储回文件。

这就是mule flow.xml的样子

<file:connector name="File" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
    <file:connector name="File1" outputPattern="sample1.txt" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
    <flow name="datatranformerFlow">
         <file:inbound-endpoint path="C:\Madhu"   name="sample.xml" responseTimeout="10000" doc:name="File" connector-ref="File"/>
         <file:file-to-string-transformer mimeType="application/xml" doc:name="File to String"/>
         <splitter expression="#[xpath3('/Names/Name')]" doc:name="Splitter"/>
         <json:xml-to-json-transformer doc:name="XML to JSON"/>
         <file:outbound-endpoint path="C:\Madhu\GV dev documents\WD files" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
     </flow>

我尝试解析的示例xml文件看起来像

<Names>
    <Name>
        <title>bnbnbha</title>
        <firstname>aa</firstname>
        <lastname>aaa</lastname>
    </Name>
    <Name>
        <title>bjkjkjk</title>
        <firstname>bb</firstname>
        <lastname>bbb</lastname>
    </Name>
    <Name>
        <title>hjhjhc</title>
        <firstname>cc</firstname>
        <lastname>ccc</lastname>
    </Name> 
    <Name>
        <title>djkjkj</title>
        <firstname>dd</firstname>
        <lastname>ddd</lastname>
    </Name> 
</Names>

当我运行mule项目时,我得到一个异常

INFO 2016-07-29 11:56:25,287 [[datatranformer] .File.receiver.01] org.mule.transport.file.FileMessageReceiver:在文件中获取的锁:C:\ Madhu \ sample.xml INFO 2016-07-29 11:56:26,193 [[datatranformer] .datatranformerFlow.stage1.02] org.mule.routing.ExpressionSplitter:表达式不计算为可拆分的类型:java.lang.String 错误:&#39;意外的角色&#39; b&#39; (代码98)在prolog;预期&#39;&lt;&#39;  在[row,col {unknown-source}]:[2,3]&#39; ERROR 2016-07-29 11:56:26,272 [[datatranformer] .datatranformerFlow.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy:

消息:com.ctc.wstx.exc.WstxUnexpectedCharException:意外字符&#39; b&#39; (代码98)在prolog;预期&#39;&lt;&#39;  at [row,col {unknown-source}]:[2,3](javax.xml.transform.TransformerException) 有效载荷:         bnbnbha         AA         AAA

我有什么问题吗?

4 个答案:

答案 0 :(得分:1)

如果您的目的只是读取XML文件,将其转换为JSON,并将其存储到文件(无需进一步处理),然后删除文件到字符串转换器和 Splitter 流量控制。

所以你的配置变成这样:

<flow name="datatranformerFlow">
     <file:inbound-endpoint path="C:\Madhu" name="sample.xml" responseTimeout="10000" doc:name="File" connector-ref="File"/>
     <json:xml-to-json-transformer doc:name="XML to JSON"/>
     <file:outbound-endpoint path="C:\Madhu\GV dev documents\WD files" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
 </flow>

答案 1 :(得分:0)

你能使用#[xpath3(&#39; / Names&#39;)]

答案 2 :(得分:0)

只需删除拆分器表达式,如前所述,您只想将此文件转换为csv,请检查以下流程:

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
	xmlns:file="http://www.mulesoft.org/schema/mule/file" 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" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
  <file:connector name="File" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
    <file:connector name="File1" outputPattern="sample1.txt" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
    <flow name="datatranformerFlow">
        <file:inbound-endpoint path="C:\madhu" connector-ref="File" responseTimeout="10000" doc:name="File"/>
        <file:file-to-string-transformer doc:name="File to String"/>
        <json:xml-to-json-transformer mimeType="application/json" doc:name="XML to JSON"/>
        <logger message="payload--&gt; #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
     <file:outbound-endpoint path="C:\Madhu\GV dev documents\WD files" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
     </flow>
</mule>

希望这有帮助!

答案 3 :(得分:0)

有很多方法可以做到这一点

  1. 您可以使用XPath
  2. 您可以通过转换消息组件
  3. 来实现
  4. 您可以通过JSON转换为XML转换器