Domanda

Sto cercando di estrarre tag da un file XML e scrivere ciascuno a un file separato in base a un attributo.

La parte di estrazione non è così difficile:

*Main> ifs <- runX ( readDocument [withCurl [],withExpat yes] "file.xml" >>> getElement "TagName" >>> getAttrValue "Name" &&& this)
*Main> :t ifs
ifs :: [(String, XmlTree)]

Ho provato a mappare Writedocument sulle seconde voci ma non ho avuto successo. Capisco che devo riportarlo in qualche modo nella monade Io ... ma non ho idea di come raggiungere questo obiettivo.


Ai fini del test ho estratto su quegli xmltrees dal risultato:

*Main> let x = (map snd ifs) !! 0
*Main> :t x
x :: XmlTree

Posso eseguire frecce x come questo:

*Main> runLA (getName) x
["TagName"]

Ma quando provo a scriverlo in un file ricevo un messaggio di errore che indica che non sono nella monaca IO (penso):

*Main> runLA (writeDocument [] "test.xml") x

<interactive>:1:8:
    Couldn't match expected type `LA a0 b0'
                with actual type `IOSLA (XIOState s0) XmlTree XmlTree'
    Expected type: LA a0 b0
      Actual type: IOStateArrow s0 XmlTree XmlTree
    In the return type of a call of `writeDocument'
    In the first argument of `runLA', namely
      `(writeDocument [] "test.xml")'

Mutevole runLA a runIOSLA Non aiuta:

*Main> runIOSLA (writeDocument [] "test.xml") x

<interactive>:1:40:
    Couldn't match expected type `XIOState s0'
            with actual type `Data.Tree.NTree.TypeDefs.NTree XNode'
    Expected type: XIOState s0
      Actual type: XmlTree
    In the second argument of `runIOSLA', namely `x'
    In the expression: runIOSLA (writeDocument [] "test.xml") x

È il più possibile.

Aggiornare

Come sottolinea Travis Brown, è possibile farlo in una freccia:

runX . applyA $ readDocument [withCurl [],withExpat yes] "file.xml" >>> getElement "Tag" >>> getAttrValue "DEF" &&& this >>> arr (\ (n,x) -> root [] [constA x] >>> writeDocument [] n)
È stato utile?

Soluzione

Uso root e constA Per fare una freccia di root del documento dal tuo nodo:

runX (root [] [constA x] >>> writeDocument [] "test.xml")

Questo dovrebbe funzionare come previsto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top