DataWeave 没有正确读取 XML

问题描述 投票:0回答:1

我正在尝试读取 XML 文件并运行一些格式以将其转换为纯文本文件,但 DataWeave 无法正确读取 XML 文件并出现错误。

这是我的输入文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ds:tfmDataService xmlns:nxcm="urn:us:gov:dot:faa:atm:tfm:flightdatacommonmessages"
    xmlns:fdm="urn:us:gov:dot:faa:atm:tfm:flightdata"
    xmlns:ns4="urn:us:gov:dot:faa:atm:tfm:ficommondatatypes"
    xmlns:ds="urn:us:gov:dot:faa:atm:tfm:tfmdataservice"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns6="http://www.fixm.aero/tfm/3.1"
    xmlns:ns8="http://www.faa.aero/nas/3.1" xmlns:ns13="urn:us:gov:dot:faa:atm:tfm:rapttimeline"
    xmlns:ns9="urn:us:gov:dot:faa:atm:tfm:ficommonmessages2"
    xmlns:ns12="urn:us:gov:dot:faa:atm:tfm:flowinformation"
    xmlns:ns11="urn:us:gov:dot:faa:atm:tfm:ficommonmessages"
    xmlns:ns10="urn:us:gov:dot:faa:atm:tfm:tfmrequestreplytypes"
    xmlns:ns16="http://www.fixm.aero/foundation/3.0" xmlns:ns15="http://www.fixm.aero/flight/3.0"
    xmlns:ns14="http://www.fixm.aero/base/3.0"
    xmlns:nxce="urn:us:gov:dot:faa:atm:tfm:tfmdatacoreelements">
    <ds:fltdOutput>
        <fdm:fltdMessage sensitivity="R" cdmPart="true" airline="RPA" major="UAL"
            sourceFacility="TFMS" sourceTimeStamp="2023-02-27T00:02:36Z" flightRef="28062242"
            acid="RPA3489" msgType="FlightControl" fdTrigger="TMI_UPDATE" depArpt="CYYZ"
            arrArpt="KEWR" sensReason="DR">
            <fdm:ncsmFlightControl>
                <nxcm:qualifiedAircraftId userCategory="COMMERCIAL" aircraftCategory="JET">
                    <nxce:aircraftId>RPA3489</nxce:aircraftId>
                    <nxce:computerId>
                        <nxce:facilityIdentifier>TFMS</nxce:facilityIdentifier>
                        <nxce:idNumber>555</nxce:idNumber>
                    </nxce:computerId>
                    <nxce:gufi>KC81743100</nxce:gufi>
                    <nxce:igtd>2023-02-26T23:30:00Z</nxce:igtd>
                    <nxce:departurePoint>
                        <nxce:airport>CYYZ</nxce:airport>
                    </nxce:departurePoint>
                    <nxce:arrivalPoint>
                        <nxce:airport>KEWR</nxce:airport>
                    </nxce:arrivalPoint>
                </nxcm:qualifiedAircraftId>
                <nxcm:etd timeValue="2023-02-27T00:11:00Z" etdType="PROPOSED" />
                <nxcm:eta timeValue="2023-02-27T00:36:00Z" etaType="ESTIMATED" />
                <nxcm:controlIndicator>CONTROL_ACTIVE</nxcm:controlIndicator>
                <nxcm:ncsmControlData>
                    <nxcm:ctd>2023-02-27T00:11:00Z</nxcm:ctd>
                    <nxcm:cta>2023-02-27T00:36:00Z</nxcm:cta>
                    <nxcm:octd>2023-02-27T00:14:00Z</nxcm:octd>
                    <nxcm:octa>2023-02-27T01:18:00Z</nxcm:octa>
                    <nxcm:controlElement>EWR</nxcm:controlElement>
                    <nxcm:controlProgramType>GDP</nxcm:controlProgramType>
                </nxcm:ncsmControlData>
            </fdm:ncsmFlightControl>
        </fdm:fltdMessage>
    </ds:fltdOutput>
</ds:tfmDataService>

这是 DataWeave 脚本:

%dw 2.0
import * from dw::core::Strings
var myInput = read(payload, 'application/xml')
output text/plain
---
"abc"

我一直收到这个错误:

You called the function 'AnonymousFunction' with these arguments: 
  1: Object (do {ns ds urn:us:gov:dot:faa:atm:tfm:tfmdataservice---{ds#tfmDataService: {ds...)
  2: String ("application/xml")

But it expects arguments of these types:
  1: String | Binary
  2: String
  3: Object


3| var myInput = read(payload, 'application/xml')
                 ^^^^
Trace:
  at main::main (line: 3, column: 15)

我不确定转型的哪一部分失败了。

xml mule dataweave mule4
1个回答
0
投票

DataWeave 正在正确读取 XML 输入问题是脚本对输入的期望不正确。

错误信息解释了问题所在:

You called the function 'AnonymousFunction' with these arguments: 
  1: Object (do {ns ds urn:us:gov:dot:faa:atm:tfm:tfmdataservice---{ds#tfmDataService: {ds...)
  2: String ("application/xml")

有效载荷——第一个参数——已经从 XML 中读取,所以现在是一个 DataWeave 对象,而不是包含在字符串或二进制流中的 XML。通过使用

read()
函数,你试图解析它两次,这是行不通的。

问题中提供的信息不清楚原因。我会假设有效载荷来自任何来源,或者流程中的先前操作已经解析了它。

所以你可以删除

read()
操作,但是如果你尝试以
text/plain
的输出格式输出它,它会因为类似的原因而失败。对象不能输出到文本字符串。如果这是你的意图,那么你需要使用
write()
函数(与
read()
相反)将对象写入一个简单的字符串。

例子:

%dw 2.0
output text/plain
---
write(payload, "application/xml")
© www.soinside.com 2019 - 2024. All rights reserved.