這個問題是關於如何使用xmlns
屬性等解析xml內容。我寫了代碼來解析它的工作原理。我會很感激能否做得更好的指針。用XML解析名稱空間的元素
我有如下一個XML文件test.xml
:
{-# LANGUAGE OverloadedStrings #-}
import Prelude hiding (readFile)
import Text.XML
import Text.XML.Cursor
import qualified Data.Text as T
import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy (fromStrict)
main :: IO()
main = do
doc <- readFile def "test.xml"
let cursor = fromDocument doc
res = fromStrict $ T.concat $ child cursor >>= laxElement "Body" >>= child >>= laxElement "SomeResponse" >>= child >>= laxElement "SomeResult" >>= descendant >>= content
pres = parseText_ def res
cursor2 = fromDocument pres
res2 = child cursor2 >>= element "head" >>= child >>= element "title" >>= descendant >>= content
print $ res2
產出ghci
:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<SomeResponse xmlns="https://testsomestuff.org/API/WS/">
<SomeResult>
<html>
<head>
<title>My <b>Title</b></title>
</head>
<body>
<p>Foo bar baz</p>
</body>
</html>
</SomeResult>
</SomeResponse>
</soap:Body></soap:Envelope>
我使用xml-conduit
寫的代碼來解析 「SomeResult」 內容解析正確:
*Main> main
["My ","Title"]
是laxElement
應用程序蟑螂找到SomeResult
內容的好方法嗎?如果有更好的方法,我會非常感謝這方面的指針。
此外,我需要做相反方向的HTTP編碼(當建立一個上面的響應請求時)內部身體被轉義的地方(如SomeResult
在text.xml
)。當使用Text.XML
構建請求時,默認情況下是否需要處理這些內容,或者是否必須通過使用類似html-entities的內容來明確地將內部體轉換爲轉義http?加上xml-conduit