2016-07-29 77 views
0

我想解析从磁盘读取的简单XMl文件并将其转换为JSON并将其存储回使用Mulesoft的文件。使用mulesoft解析XML文件

这是骡子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> 

容貌,当我运行骡子项目,我得到一个例外

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


消息:com.ctc.wstx.exc.WstxUnexpectedCharException:意外字符 'b'(代码98)在序言;预计 '<' 在[行,列{未知来源}] [2,3](javax.xml.transform.TransformerException中) 有效载荷: bnbnbha AA AAA

有什么我在做什么错了?

回答

0

你能只使用#[xpath3(“/名称”)]

1

如果你的目的只是读取XML文件,将其转换成JSON,并将其存储到文件(无需进行进一步处理),然后取出文件到字符串变压器和分流器流量控制。

所以你的配置变成了这个样子:

<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> 
+0

这一个作品..谢谢 –

0

只需卸下分流表达,正如你所提到的,你只是想将这个文件转换成CSV,请检查下面的流程:

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

 
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" 
 
\t 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" 
 
\t xmlns:spring="http://www.springframework.org/schema/beans" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t 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>

希望这有助于!

0

这样做有它

  1. 您可以使用XPath的许多方面
  2. 你可以做到这一点通过变换消息组件
  3. 您可以通过JSON这样做是为了XML转换器