Remy
Remy

Reputation: 1

How to extract a node value from an xml in Mule

This is my source xml sample.

<?xml version="1.0" encoding="UTF-8"?>
<XML>
    <Meta>
        <Status>Success</Status>
        <Debug></Debug>
    </Meta>
    <Result>
        <Surveys>
            <element id='0'>
                <responses>6</responses>
                <SurveyType>SS</SurveyType>
                <SurveyID>SV_01C7i5l62dnKTel</SurveyID>
                <SurveyName>Georgia Dome GS</SurveyName>
                <SurveyDescription>Georgia Dome</SurveyDescription>
                <SurveyOwnerID>UR_8IZEh6bVlQaF41L</SurveyOwnerID>
                <DivisionID>DV_2cmHYrtm8C93T6J</DivisionID>
                <SurveyStatus>Active</SurveyStatus>
                <SurveyStartDate>0000-00-00 00:00:00</SurveyStartDate>
                <SurveyExpirationDate>0000-00-00 00:00:00</SurveyExpirationDate>
                <SurveyCreationDate>2014-06-18 15:14:48</SurveyCreationDate>
                <CreatorID>UR_8IZEh6bVlQaF41L</CreatorID>
                <LastModified>2014-10-24 14:01:23</LastModified>
                <LastActivated>2014-06-24 09:39:23</LastActivated>
                <GroupName>Analytics</GroupName>
                <GroupID>GR_3kMZEX6m1IqqSjP</GroupID>
                <UserLastName>Parrott-Sheffer</UserLastName>
                <UserFirstName>Brandon</UserFirstName>
            </element>

I would like to print a list of all the value that come in the tag - <SurveyID>.

The following is my Mule flow:

</flow>
</flow>
<flow name="testFlow1" doc:name="testFlow1">
    <file:inbound-endpoint path="C:\Data\Mule\deploy\out" responseTimeout="10000" doc:name="File"/>
    <file:file-to-string-transformer doc:name="File to String"/>
    <logger message="*********First Message - *********   #[message.payload.toString()]" level="ERROR" doc:name="Logger"/>
    <foreach collection="#[xpath('//XML/Result/Surveys/element')]" doc:name="For Each">

        <set-variable doc:name="Variable" value="#[xpath('SurveyID/text()').text]" variableName="id"/>
        <logger level="INFO" message="********* The ID is - #[flowVars['id']]" doc:name="Logger"/>
    </foreach>
</flow>

But the result I see on the console is -

INFO 2015-01-20 17:03:35,527 [[REST-API].testFlow1.stage1.02] org.mule.transformer.simple.AddFlowVariableTransformer: Variable with key "id", not found on message using "#[xpath('//SurveyID/text()').text]".Since the value was marked optional, nothing was set on the message for this variable
INFO 2015-01-2017:03:35,527 [[REST-API].testFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor: null

I am getting this xml from a third party and I noticed it does not have any namespace information. Can you please help correct my xpath and display the values.

I am using Mule studio 3.5

Upvotes: 0

Views: 6859

Answers (3)

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

First of all I need to say your XML is not valid as it doesn't have ending tags as mentioned by Victor Anyways,if you make it correct then, You can easily get the value of all SurveyID nodes using XPATH3 and splitter

<flow name="testFlow">
   <http:listener config-ref="HTTP_InboundRequest"  path="/test" doc:name="HTTP"/>
   <splitter expression="#[xpath3('//XML/Result/Surveys/element',message.payload,'NODESET')]" doc:name="Splitter"/>
   <logger level="INFO" message="#[xpath3('SurveyID')]" doc:name="Logger"/>
</flow>

It will successfully print all the values of SurveyID

Upvotes: 1

V&#237;ctor Romero
V&#237;ctor Romero

Reputation: 5115

The xpaths you are using should work, although they could be optimized.

The sample xml looks ok too except for the fact that lacks the closing of three elements:

</Surveys>
</Result>
</XML>

My guess is that sometimes it fails for your depending on the input. I do recommend to put a

<logger message="#[message.payload]" level="ERROR" />

right before the foreach and inside the foreach.

Upvotes: 0

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

I am not familiar with Mule, only with XPath. But

SurveyID/text()

as in

<set-variable doc:name="Variable" value="#[xpath('SurveyID/text()').text]" variableName="id"/>

would be more logical in my opinion, because inside this for loop, it does not make sense to use an expression that starts with //. Let me know if it works.


You should also slightly modifiy the other XPath expression, also removing the //, then the whole should be

<foreach collection="#[xpath('/XML/Result/Surveys/element')]" doc:name="For Each">
            <set-variable doc:name="Variable" value="#[xpath('SurveyID/text()').text]" variableName="id"/>
            <logger level="INFO" message="#[flowVars['id']]" doc:name="Logger"/>
        </foreach>

Finally, I'm not sure why you select text nodes and then write .text. To me, it seems like you are doing the same thing twice. What does

#[xpath('SurveyID').text]

do?

Upvotes: 0

Related Questions