miniBill/elm-xml-parser/1.0.1

NOTE: the latest version is 1.0.2

XmlParser

The XML Parser.

Types

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

This represents the entire XML structure.

  • processingInstructions: <?xml-stylesheet type="text/xsl" href="style.xsl"?>
  • docType: <!DOCTYPE root SYSTEM "foo.xml">
  • root: <root><foo/></root>
{ 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 Declaration starting with "<!DOCTYPE".

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

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

DTD (Doc Type Definition)

  • Public: <!DOCTYPE root PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  • System: <!DOCTYPE root SYSTEM "foo.xml">
  • Custom: <!DOCTYPE root [ <!ELEMENT ...> ]>
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"]) }
type alias DeadEnd =
DeadEnd String Problem

A problem when parsing. See the elm/parser documentation for details.

Format

format : Xml -> String

Convert Xml into String.

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