jinjor / elm-xml-parser / XmlParser

The XML Parser.

Types


type alias Xml =
{ processingInstructions : List ProcessingInstruction
, docType : Maybe DocType
, root : Node 
}

This represents the entire XML structure.


type alias ProcessingInstruction =
{ name : String
, value : String 
}

Processing Instruction such as <?xml-stylesheet type="text/xsl" href="style.xsl"?>.

The example above is parsed as { name = "xml-stylesheet", value = "type=\"text/xsl\" href=\"style.xsl\"" }. The value (presudo attributes) should be parsed by application.


type alias DocType =
{ rootElementName : String
, definition : DocTypeDefinition 
}

Doc Type Decralation starting with "<!DOCTYPE".

This contains root element name and rest of details as DocTypeDefinition.


type DocTypeDefinition
    = Public String String (Maybe String)
    | System String (Maybe String)
    | Custom String

DTD (Doc Type Definition)


type Node
    = Element String (List Attribute) (List Node)
    | Text String

Node is either a element such as <a name="value">foo</a> or text such as foo.


type alias Attribute =
{ name : String
, value : String 
}

Attribute such as name="value"

Parse

parse : String -> Result (List DeadEnd) Xml

Parse XML string.

<?xml ... ?> and <!DOCTYPE ... > is optional so you don't need to ensure them.

> import XmlParser
> XmlParser.parse """<a name="value">foo</a>"""
Ok { processingInstructions = [], docType = Nothing, root = Element "a" ([{ name = "name", value = "value" }]) ([Text "foo"]) }

Format

format : Xml -> String

Convert Xml into String.

This function does NOT insert line breaks or indents for readability.