我正在努力尝试在以下 xml 片段中使用 HXT 解析可选元素:
<ns9:accountBankingInfo>
<key>
<accountId>10</accountId>
<bankingId>01</bankingId>
</key>
<bankingInfo>
<description>Bigbank Co.</description>
<eft>
<bankCode>222</bankCode>
<transit>111</transit>
<accountNumber>3333333</accountNumber>
</eft>
</bankingInfo>
<defaultType>ACCOUNT</defaultType> <!-- optional -->
</ns9:accountBankingInfo>
我使用 Maybe 来表示 defaultType 元素:
data BankingInfo = BankingInfo { bankingID :: String
, bankingDesc :: String
, bankCode :: String
, bankTransit :: String
, bankAccount :: String
, defaultType :: Maybe String
} deriving (Show, Eq)
我正在解析 accountBankingInfo 元素,如下所示:
bankParser :: ArrowXml a => a XmlTree BankingInfo
bankParser = deep (isElem >>> hasLocalPart "accountBankingInfo") >>> proc x -> do
i <- getText <<< getChildren <<< deep (hasName "bankingId") -< x
d <- getText <<< getChildren <<< deep (hasName "description") -< x
b <- getText <<< getChildren <<< deep (hasName "bankCode") -< x
t <- getText <<< getChildren <<< deep (hasName "transit") -< x
a <- getText <<< getChildren <<< deep (hasName "accountNumber") -< x
g <- nc "defaultType" -< x
returnA -< BankingInfo i d b t a g
nc name = ifA (deep (hasName name)) (getChildren >>> getText >>> arr Just) (arr (const Nothing))
它可以编译,但是当我解析文件时,没有返回任何 BankingInfo。如果我更改 BankingInfo 类型以删除 defaultType 并且不用担心解析该元素,那么一切都会成功。
有没有一种简单的方法来处理 XML 中的可选元素并将其转换为 Maybe x?