如何在Mule Anypoint中拆分XML?

时间:2014-09-10 10:15:59

标签: mule

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" 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" version="EE-3.5.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

    <mulexml:namespace-manager includeConfigNamespaces="true">
        <mulexml:namespace prefix="ns0" uri="http://www.cpscreen.com/schemas"/>
    </mulexml:namespace-manager>

    <configuration doc:name="Configuration">
        <expression-language autoResolveVariables="true">
            <import class="org.mule.util.StringUtils" />
            <import class="org.mule.util.ArrayUtils" />
        </expression-language>
    </configuration>

    <flow name="user_provisions_-_xml_postFlow1" doc:name="user_provisions_-_xml_postFlow1">
        <file:inbound-endpoint path="E:/temp/mule" responseTimeout="10000" doc:name="File">
            <file:filename-regex-filter pattern="in_(.*).csv" caseSensitive="true"/>
        </file:inbound-endpoint>
        <file:file-to-string-transformer doc:name="File to String"/>
        <expression-component doc:name="Expression"><![CDATA[java.util.List items =  new java.util.ArrayList( Arrays.asList( payload.split("\n") ));
items.remove(0);;

java.lang.String listString = '';

for  (String s : items) {
    listString += s + "\n";
}

payload=listString.trim();]]></expression-component>

        <http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="46908" path="userprovision" method="POST" doc:name="HTTP"/>
        <object-to-string-transformer mimeType="text/xml" doc:name="Object to String"/>
        <splitter expression="#[xpath('//UpdateUserRequest')]" doc:name="Splitter"/>

        <logger message="Here is #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

我好像,

WARN  2014-09-10 15:30:27,181 [[user_provisions_-_xml_post].user_provisions_-_xml_postFlow1.stage1.02] org.mule.routing.ExpressionSplitter: Splitter returned no results. If this is not expected, please check your split expression

更新

我遇到了问题,但我可以解决分裂问题。由于XML拆分器中的namespace不评估拆分表达式...

<?xml version="1.0" encoding="UTF-8"?>
<UpdateUserRequests xmlns="http://www.cpscreen.com/schemas">
    <UpdateUserRequest userId="Test" account="Test" password="Test">
        <User>
            <Account id="4">34567</Account>
            <UserId>Test</UserId>
            <Profile>Admin</Profile>
            <PersonName>
                <GivenName>Sahak</GivenName>
                <FamilyName>Kn</FamilyName>
            </PersonName>
        </User>
    </UpdateUserRequest>
    <UpdateUserRequest userId="Test" account="Test" password="Test">
        <User>
            <Account id="5">12345</Account>
            <UserId>Test</UserId>
            <Profile>Admin</Profile>
            <PersonName>
                <GivenName>Arun</GivenName>
                <FamilyName>Kumar</FamilyName>
            </PersonName>
        </User>
    </UpdateUserRequest>
</UpdateUserRequests>

如果我删除xmlns="http://www.cpscreen.com/schemas",则会拆分为多个

如果xml中有命名空间,我该如何添加spit表达式?

2 个答案:

答案 0 :(得分:2)

使用以下表达式将xml与命名空间分开。

<splitter expression="#[xpath('//ns0:UpdateUserRequest')]" doc:name="Splitter"/>

上面的表达式考虑了&#34; ns0&#34;命名空间在Mule MXL命名空间管理器中添加。

希望这有帮助。

答案 1 :(得分:0)

您应该在文件入站端点后直接使用拆分器... 此表达式适用于拆分CSV文件: -

<splitter expression="#[StringUtils.split(message.payload, '\n\r')]" doc:name="Splitter_For_MultipleRow"/>

如果您的CSV文件有列标题,如姓名,年龄等,您可以使用: -

<splitter expression="#[rows=StringUtils.split(message.payload, '\n\r');ArrayUtils.subarray(rows,1,rows.size())]" doc:name="Splitter_For_MultipleRow"></splitter>