Madhuraj S V
Madhuraj S V

Reputation: 13

Parsing an XML file using mulesoft

I am trying to parse an Simple XMl file read from disk and convert that to JSON and store it back to a file using Mulesoft.

This is how the mule flow.xml looks like

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

The sample xml file that i am trying to parse looks like

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

When i run the mule project, i am getting an exception

INFO 2016-07-29 11:56:25,287 [[datatranformer].File.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\Madhu\sample.xml INFO 2016-07-29 11:56:26,193 [[datatranformer].datatranformerFlow.stage1.02] org.mule.routing.ExpressionSplitter: The expression does not evaluate to a type that can be split: java.lang.String ERROR: 'Unexpected character 'b' (code 98) in prolog; expected '<' at [row,col {unknown-source}]: [2,3]' ERROR 2016-07-29 11:56:26,272 [[datatranformer].datatranformerFlow.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy:


Message : com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character 'b' (code 98) in prolog; expected '<' at [row,col {unknown-source}]: [2,3] (javax.xml.transform.TransformerException) Payload : bnbnbha aa aaa

Is there something i am doing wrong ?

Upvotes: 1

Views: 1913

Answers (4)

Anil Agrawal
Anil Agrawal

Reputation: 11

There are many ways of doing it

  1. you can use XPath
  2. you can do it by transform message component
  3. you can do it by JSON to XML converter

Upvotes: 0

Mahesh Sharma
Mahesh Sharma

Reputation: 11

Just remove the splitter expression, as you have mentioned you just want to convert this file into csv, please check the below flow:

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

Hope this helps!

Upvotes: 0

sulthony h
sulthony h

Reputation: 1277

If your purpose is only reading an XML file, convert it to JSON, and store it to file (without further process) then remove the File to String transformer and the Splitter flow control.

So you configuration become like this:

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

Upvotes: 1

Alex Fernandez
Alex Fernandez

Reputation: 1942

Can you just use #[xpath3('/Names')]

Upvotes: 0

Related Questions