如何从Mule中的xml中提取节点值 [英] How to extract a node value from an xml in Mule

查看:99
本文介绍了如何从Mule中的xml中提取节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的源xml示例.

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>

我想打印出标签-<SurveyID>中所有值的列表.

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

以下是我的M子流:

</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:带有变量 在使用的消息中找不到键"id" #[xpath('//SurveyID/text()').text]".由于该值被标记为可选, 在此变量的消息上未设置任何内容
INFO 2015-01-2017:03:35,527 [[REST-API] .testFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor:null

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

我从第三方获得此xml,但我注意到它没有任何名称空间信息.您能帮我纠正我的xpath并显示值吗?

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.

我正在使用Mule studio 3.5

I am using Mule studio 3.5

推荐答案

首先,我要说您的XML无效,因为它没有Victor提到的结尾标记 无论如何,如果您将其正确设置,则可以使用

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>

它将成功打印出SurveyID的所有值

It will successfully print all the values of SurveyID

这篇关于如何从Mule中的xml中提取节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆