Mulesoft:不存在的深节点的默认值

时间:2016-09-28 19:12:39

标签: mule

我使用Data Weave转换XML并且不存在现有节点的问题。例如xml

<a>
   <b>
      <c>
         value
      </c>
   </b>
</a> 

提取{one:$ .a,two:$ a.b,three:$ a.b.c}当所有节点a,b和c都存在时,它可以正常工作。

确定节点b何时为空且具有节点c的默认值的方法是什么?

这只是一个例子。真实数据有很多深度级别,每个级别都可能为空。也许我应该使用when / otherwise但是我应该为每个变量重复整个数据结构。

enter image description here

这是骡子流:

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

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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.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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <flow name="xml2flatFlow" doc:description="To run copy src/main/resources/*.xml files to src/test/resources/FileIn
Good working example is xml_in_good.xml

Issues:

1) Should be working with Streams but from Json2Flat there is no way to FileOut. &quot;Load everything to memory&quot; is required but it kills stream. What is the way to make it Stream and work with huge files?

2) FlatFile field have particular length. Transformation should cut the length. Instead it produces exception. Should be option oe way to force the length of the string.
Example: xml_in_bad_too_long.xml

3) Main issue - I cannot figure out how process empty nodes. 
Example: xml_in_bad.xml

Provided example is simlpiest. In reality I have very long xpath like payload.a.b.c.d.e.f.g.h
I tried to do when/otherwise, default, h?, internal Java program - nothing works.

I believe that processor File2Json is not necessary but I'd added it to make sure XML processor have no influence on the flow. ">
        <file:inbound-endpoint path="src\test\resources\FileIn" moveToDirectory="src\test\resources\FileProcessed" responseTimeout="10000" doc:name="FileIn"/>
        <dw:transform-message metadata:id="24cbc998-ba7d-44f7-a3fa-cb502d352e87" doc:name="File2Json">
            <dw:input-payload mimeType="application/xml"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
        </dw:transform-message>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <dw:transform-message metadata:id="15c2b229-fb5a-4430-be89-c446e79bfa2e" doc:name="Json2Json">
            <dw:input-payload doc:sample="C:\Users\Alex\AnypointStudio6\workspace\xml2flat\src\main\resources\too_long.json" mimeType="application/json"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
    a: payload.a default {},
    b: payload.a.b default {},
    c: payload.a.b.c default ""
}]]></dw:set-payload>
        </dw:transform-message>
        <dw:transform-message metadata:id="8ef7c277-ec47-4564-9ece-eac11c27c891" doc:name="Json2Flat">
            <dw:input-payload doc:sample="C:\Users\Alex\AnypointStudio6\workspace\xml2flat\src\main\resources\too_long.json"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output text/plain schemaPath = "flatfile.ffd" , segmentIdent = ""
---
[{
    field_0: payload.c default "---"
}]]]></dw:set-payload>
        </dw:transform-message>
        <object-to-string-transformer doc:name="Load everythin to memory"/>
        <file:outbound-endpoint path="src/test/resources/FileOut" outputPattern="out.txt" responseTimeout="10000" doc:name="FileOut"/>
    </flow>
</mule>

这是log:

*******************************************************************************************************
* xml2flatSO                                    * default                        * DEPLOYED           *
*******************************************************************************************************

INFO  2016-09-30 13:57:29,271 [[xml2flatSO].connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\Users\Alex\AnypointStudio6\workspace\xml2flatSO\src\test\resources\FileIn\xml_in_good.xml
INFO  2016-09-30 13:57:37,892 [[xml2flatSO].xml2flatFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {
  "a": {
    "b": {
      "c": "GOOD"
    }
  }
}
INFO  2016-09-30 13:57:42,334 [[xml2flatSO].connector.file.mule.default.dispatcher.01] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'connector.file.mule.default.dispatcher.546298364'. Object is: FileMessageDispatcher
INFO  2016-09-30 13:57:42,334 [[xml2flatSO].connector.file.mule.default.dispatcher.01] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'connector.file.mule.default.dispatcher.546298364'. Object is: FileMessageDispatcher
INFO  2016-09-30 13:57:42,335 [[xml2flatSO].connector.file.mule.default.dispatcher.01] org.mule.transport.file.FileConnector: Writing file to: C:\Users\Alex\AnypointStudio6\workspace\xml2flatSO\src\test\resources\FileOut\out.txt
INFO  2016-09-30 13:57:52,448 [[xml2flatSO].connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\Users\Alex\AnypointStudio6\workspace\xml2flatSO\src\test\resources\FileIn\xml_in_bad.xml
INFO  2016-09-30 13:57:55,979 [[xml2flatSO].xml2flatFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {
  "a": ""
}
ERROR 2016-09-30 13:58:16,403 [[xml2flatSO].xml2flatFlow.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Exception while executing: 
    b: payload.a.b default {},
       ^
Type mismatch for 'Value Selector' operator
     found :string, :name
  required :datetime, :name or
  required :localdatetime, :name or
  required :object, :name or
  required :time, :name or
  required :array, :name or
  required :date, :name or
  required :localtime, :name or
  required :period, :name
Payload               : {
  "a": ""
}
Payload Type          : java.lang.String
Element               : /xml2flatFlow/processors/3 @ xml2flatSO:xml2flat.xml:37 (Json2Json)
Element XML           : <dw:transform-message metadata:id="15c2b229-fb5a-4430-be89-c446e79bfa2e" doc:name="Json2Json">
                        <dw:input-payload doc:sample="C:\Users\Alex\AnypointStudio6\workspace\xml2flat\src\main\resources\too_long.json" mimeType="application/json"></dw:input-payload>
                        <dw:set-payload>%dw 1.0%output application/json---{a: payload.a default {},b: payload.a.b default {},c: payload.a.b.c default ""}</dw:set-payload>
                        </dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
com.mulesoft.weave.engine.ast.dynamic.UnexpectedOperationTypesException: Type mismatch for 'Value Selector' operator
     found :string, :name
  required :datetime, :name or
  required :localdatetime, :name or
  required :object, :name or
  required :time, :name or
  required :array, :name or
  required :date, :name or
  required :localtime, :name or
  required :period, :name
    at com.mulesoft.weave.engine.ast.OpNode$class.doExecuteAutoCoercion(OpNode.scala:79)
    at com.mulesoft.weave.engine.ast.BinaryOpNode.doExecuteAutoCoercion(BinaryOpNode.scala:8)
    at com.mulesoft.weave.engine.ast.OpNode$class.doExecute(OpNode.scala:33)
    at com.mulesoft.weave.engine.ast.BinaryOpNode.doExecute(BinaryOpNode.scala:8)
    at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
    at com.mulesoft.weave.engine.ast.BinaryOpNode.execute(BinaryOpNode.scala:8)
    at com.mulesoft.weave.engine.ast.selectors.NullSafeNode.doExecute(NullSafeNode.scala:16)
    at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
    at com.mulesoft.weave.engine.ast.selectors.NullSafeNode.execute(NullSafeNode.scala:10)
    at com.mulesoft.weave.engine.ast.conditional.DefaultNode.doExecute(DefaultNode.scala:13)
    at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
    at com.mulesoft.weave.engine.ast.conditional.DefaultNode.execute(DefaultNode.scala:10)
    at com.mulesoft.weave.engine.ast.structure.KeyValuePairNode.doExecute(KeyValuePairNode.scala:12)
    at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
    at com.mulesoft.weave.engine.ast.structure.KeyValuePairNode.execute(KeyValuePairNode.scala:10)
    at com.mulesoft.weave.engine.ast.structure.ObjectNode$$anonfun$4.apply(DynamicObjectNode.scala:39)
    at com.mulesoft.weave.engine.ast.structure.ObjectNode$$anonfun$4.apply(DynamicObjectNode.scala:38)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.Iterator$class.foreach(Iterator.scala:742)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1194)
    at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at com.mulesoft.weave.engine.ast.structure.ObjectNode.doExecute(DynamicObjectNode.scala:38)
    at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
    at com.mulesoft.weave.engine.ast.structure.ObjectNode.execute(DynamicObjectNode.scala:28)
    at com.mulesoft.weave.engine.ast.ValueNode$class.write(AstNode.scala:28)
    at com.mulesoft.weave.engine.ast.structure.ObjectNode.write(DynamicObjectNode.scala:28)
    at com.mulesoft.weave.engine.Engine.write(Engine.scala:165)
    at com.mulesoft.weave.engine.Engine.execute(Engine.scala:88)
    at com.mulesoft.weave.engine.Engine.execute(Engine.scala:103)
    at com.mulesoft.weave.mule.debug.DebugWeaveExecutorStrategy$$anonfun$execute$1.apply(DebugWeaveExecutorStrategy.scala:30)
    at com.mulesoft.weave.mule.debug.DebugWeaveExecutorStrategy$$anonfun$execute$1.apply(DebugWeaveExecutorStrategy.scala:28)
    at scala.Option.map(Option.scala:146)
    at com.mulesoft.weave.mule.debug.DebugWeaveExecutorStrategy.execute(DebugWeaveExecutorStrategy.scala:28)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:117)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:65)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:108)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
    at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:88)
    at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
    at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:98)
    at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
    at org.mule.interceptor.AbstractEnvelopeInterceptor.processBlocking(AbstractEnvelopeInterceptor.java:58)
    at org.mule.processor.AbstractRequestResponseMessageProcessor.process(AbstractRequestResponseMessageProcessor.java:47)
    at org.mule.processor.AsyncInterceptingMessageProcessor.processNextTimed(AsyncInterceptingMessageProcessor.java:123)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker$1.process(AsyncInterceptingMessageProcessor.java:208)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker$1.process(AsyncInterceptingMessageProcessor.java:201)
    at org.mule.execution.ExecuteCallbackInterceptor.execute(ExecuteCallbackInterceptor.java:16)
    at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:35)
    at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:22)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:30)
    at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:14)
    at org.mule.execution.BeginAndResolveTransactionInterceptor.execute(BeginAndResolveTransactionInterceptor.java:67)
    at org.mule.execution.ResolvePreviousTransactionInterceptor.execute(ResolvePreviousTransactionInterceptor.java:44)
    at org.mule.execution.SuspendXaTransactionInterceptor.execute(SuspendXaTransactionInterceptor.java:50)
    at org.mule.execution.ValidateTransactionalStateInterceptor.execute(ValidateTransactionalStateInterceptor.java:40)
    at org.mule.execution.IsolateCurrentTransactionInterceptor.execute(IsolateCurrentTransactionInterceptor.java:41)
    at org.mule.execution.ExternalTransactionInterceptor.execute(ExternalTransactionInterceptor.java:48)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:28)
    at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:13)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:110)
    at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:30)
    at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker.doRun(AsyncInterceptingMessageProcessor.java:200)
    at org.mule.work.AbstractMuleEventWork.run(AbstractMuleEventWork.java:53)
    at org.mule.work.WorkerContext.run(WorkerContext.java:286)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

********************************************************************************

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您可以使用类似

的默认关键字
%dw 1.0
%output application/json
---
 {  one: payload.a, 
    two: payload.a.b default "y", 
    three: payload.a.b.c default "x"
 } 

输入: -

<a>
    <c>value</c>
</a>

输出

{
  "one": {
    "c": "value"
  },
  "two": "y",
  "three": "x"
}

希望这有帮助。

答案 1 :(得分:0)

我曾与Mulesoft公开过问题,他们提供了这个有效的建议。 应将某些属性添加到Dataweave,然后根据需要运行。

<dw:reader-property name="nullValueOn" value="blank" />

无论提供多少数据 a.b.c a.b a 或根本没有提供任何数据,这三种情况都有效。

%dw 1.0
%output application/json
---
 {  one: payload.a, 
    two: payload.a.b default "y", 
    three: payload.a.b.c default "x"
 }