【问题标题】:Parsing an XML file using mulesoft使用 mulesoft 解析 XML 文件
【发布时间】:2016-12-03 19:19:04
【问题描述】:

我正在尝试解析从磁盘读取的 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 错误:'序言中的意外字符'b'(代码98);预期'


消息:com.ctc.wstx.exc.WstxUnexpectedCharException:序言中出现意外字符“b”(代码 98);预期'

是不是我做错了什么?

【问题讨论】:

    标签: file parsing mule


    【解决方案1】:

    有很多方法

    1. 您可以使用 XPath
    2. 你可以通过转换消息组件来做到这一点
    3. 您可以通过 JSON 到 XML 转换器来实现

    【讨论】:

      【解决方案2】:

      只需删除拆分器表达式,正如您所说,您只想将此文件转换为 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】:

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

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

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

        【讨论】:

          【解决方案4】:

          你能用#[xpath3('/Names')]

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-03-27
            • 2012-07-15
            • 2013-05-29
            • 2012-12-16
            • 2011-09-01
            • 2021-01-02
            • 2017-10-28
            • 2020-12-30
            相关资源
            最近更新 更多