billstclair / elm-xml-eeue56 / Xml

The main data structure along with some trivial helpers.


type Value
    = Tag String (Dict String Value) Value
    | StrNode String
    | IntNode Basics.Int
    | FloatNode Basics.Float
    | BoolNode Basics.Bool
    | NullNode
    | Object (List Value)
    | DocType String (Dict String Value)

Representation of the XML tree

foldl : (Value -> a -> a) -> a -> Value -> a

Standard foldl

map : (Value -> Value) -> Value -> Value

Standard mapping of a value to another value

XML/JSON conversion

This library can convert to and from Json.Value.

xmlToJson2 : Value -> Json.Encode.Value

Convert an Xml.Value to a Json.Value

The conversion of Tags changed in version 2.0.0, so that they can be properly converted back. The attribute names become JSON keys with "@" prepended, and the value becomes a JSON key named "#value": 1 becomes {foo: {#value: 1, @x: "x"}}

Renamed from xmlToJson to force a bump to version 2.0.

import Dict
import Json.Encode as Json
import Xml exposing (jsonToXml, Value(..))

xmlToJson2 (StrNode "hello")
--> Json.string "hello"

xmlToJson2 (IntNode 5)
--> Json.int 5

xmlToJson2 (FloatNode 5)
--> Json.float 5

xmlToJson2 (BoolNode True)
--> Json.bool True

xmlToJson2 (Object [ IntNode 5, BoolNode True ])
--> Json.list identity [Json.int 5, Json.bool True]

xmlToJson2 (Tag "foo" (Dict.fromList [("x",StrNode "x")]) (IntNode 1))
--> Json.object [("foo", Json.object [("#value", Json.int 1), ("@x", Json.string "x")])]

xmlToJson2 (DocType "" Dict.empty)
--> Json.null

xmlToJson2 NullNode
--> Json.null

jsonToXml : Json.Encode.Value -> Value

Convert a Json.Value into an Xml.Value

import Dict
import Json.Encode as Json
import Xml exposing (jsonToXml, Value(..))

jsonToXml (Json.string "hello")
--> StrNode "hello"

jsonToXml (Json.int 5)
--> IntNode 5

jsonToXml (Json.float 10.5)
--> FloatNode 10.5

jsonToXml (Json.bool True)
--> BoolNode True

jsonToXml (Json.object [("name", Json.string "hello")])
--> Tag "name" Dict.empty (StrNode "hello")

jsonToXml (Json.list identity [Json.string "name", Json.string "hello"])
--> Object [ StrNode "name", StrNode "hello" ]

jsonToXml (Json.object [("foo", Json.object [("#value", Json.int 1), ("@x", Json.string "x")])])
--> Tag "foo" (Dict.fromList [("x",StrNode "x")]) (IntNode 1)

xmlDecoder : Json.Decode.Decoder Value

A decoder for XML

isValidXmlName : String -> Basics.Bool

Test that a string is a valid XML name.

This enforces the rules for XML tag and attribute names, as well as for the names of several less common constructs.

See O'Reilly: XML in a Nutshell: 2.4 XML Names.

XML character entities

This library can encode and decode the five predefined XML character entities. Numeric character references and other named HTML entities (e.g. € or €) are currently not supported. Please try to use UTF-8 / Unicode instead.

decodeXmlEntities : String -> String

Decode string with XML entities

decodeXmlEntities "<hello>"
--> "<hello>"

Do not decode entities twice!

decodeXmlEntities "&amp;lt;hello&gt;"
--> "&lt;hello>"

encodeXmlEntities : String -> String

Encode string with XML entities

encodeXmlEntities "<hello>"
--> "&lt;hello&gt;"