如何从Mule中的xml中提取节点值

时间:2015-01-20 22:11:00

标签: xpath mule

这是我的源xml示例。

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

以下是我的骡子流程:

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

但我在控制台上看到的结果是 -

  

INFO 2015-01-20 17:03:35,527 [[REST-API] .testFlow1.stage1.02]   org.mule.transformer.simple.AddFlowVariableTransformer:变量   密钥&#34; id&#34;,在使用消息时找不到   &#34;#[xpath(&#39; // SurveyID / text()&#39;)。text]&#34;。由于值被标记为可选,   此变量的消息没有设置任何内容   INFO 2015-01-2017:03:35,527 [[REST-API] .testFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor:null

我从第三方获取此xml,我注意到它没有任何命名空间信息。你能帮忙纠正我的xpath并显示值。

我正在使用Mule studio 3.5

3 个答案:

答案 0 :(得分:1)

首先,我需要说你的XML无效,因为它没有Victor提到的结束标签 无论如何,如果你做得正确那么,你可以使用XPATH3和分割器轻松获得所有 SurveyID 节点的值

<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的所有值

答案 1 :(得分:0)

我不熟悉Mule,只有XPath。但

SurveyID/text()

,如

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

会更合乎逻辑,因为在这个for循环中,使用以//开头的表达式是没有意义的。如果有效,请告诉我。


您还应该稍微修改其他XPath表达式,同时删除//,然后整个应该是

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

最后,我不确定为什么选择文本节点然后编写.text。对我而言,似乎你两次做同样的事情。什么

#[xpath('SurveyID').text]

办?

答案 2 :(得分:0)

您正在使用的xpath应该工作,尽管它们可以进行优化。

示例xml看起来也不错,除了缺少三个元素的关闭这一事实:

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

我的猜测是,有时它会因你的输入而失败。我建议放一个

<logger message="#[message.payload]" level="ERROR" />
就在foreach之前和foreach内部。