cromo The Cromo Guide
Download PDF IT

std.xml

XML, read and written: the answers of the services that speak it, as S3, SOAP and the feeds of RSS, and the files of settings. parse gives the tree of the elements of a text, and encode and pretty write a tree as text.

cromo
import std.xml

xml.Element list = xml.parse("<list><item id=\"7\">pen</item><item id=\"8\">ink</item></list>")
for (xml.Element item in list.all("item")) {
    println("{} {}", item.attribute("id"), item.text)     // 7 pen, 8 ink
}

xml.Element made = xml.element("order")
made.setAttribute("id", "42")
made.addText("item", "pen & ink")
println("{}", xml.encode(made))    // <?xml ...?><order id="42"><item>pen &amp; ink</item></order>

The guide: JSON.

Made from the documentation in the sources of the library.

#Types

#Attribute

cromo
struct Attribute

An attribute of an element.

  • string name: "id", or with its prefix, "xml:lang"
  • string value: without the quotes and with the entities read: "a & b"

#Element

cromo
struct Element

An element of XML: its name, its attributes, its text and the elements inside it. One declared without a value has no name: it is what child gives when there is no such element, so that a chain as root.child("a").child("b").text gives "" and does not throw.

  • string name: "item", or with its prefix, "s:Body"
  • list<Attribute> attributes: in their order
  • string text: the text right inside it, with the entities read
  • list<Element> children: the elements inside it, in their order

#Element.localName

cromo
string localName() const

The name without its prefix: "Body" for "s:Body".

#Element.attribute

cromo
string attribute(string key) const

The value of the attribute key, or "" when the element has none.

#Element.hasAttribute

cromo
bool hasAttribute(string key) const

Whether the element has the attribute key.

#Element.setAttribute

cromo
void setAttribute(string key, string value)

Gives the attribute key a value, in place of the one it had or as a new one.

#Element.has

cromo
bool has(string key) const

Whether an element named key is right inside this one.

#Element.child

cromo
Element child(string key) const

The first element named key right inside this one, or an element without a name when there is none.

#Element.textOf

cromo
string textOf(string key) const

The text of the first element named key right inside this one, or "" when there is none: entry.textOf("Size") for <Size>12</Size>.

#Element.all

cromo
list<Element> all(string key) const

The elements named key right inside this one, in their order.

#Element.add

cromo
void add(move Element inside)

Puts an element inside this one, after the others.

#Element.addText

cromo
void addText(string key, string content)

Puts inside this one an element named key with a text: part.addText("ETag", tag) for <ETag>...</ETag>.

#Functions

#element

cromo
Element element(string name)

An element named name, with nothing in it, to fill and to write.

#escape

cromo
string escape(string text)

The text with the characters that XML keeps for itself written as entities: &, <, > and the two quotes. xml.escape("a < b") gives "a &lt; b".

#encode

cromo
string encode(Element root)

The document of the element as text on one line, in UTF-8, with the declaration of XML in front. Throws an Error for a name of an element or of an attribute that XML does not take, as an empty one or one with a space.

#pretty

cromo
string pretty(Element root)

The document of the element as text for a person: an element on each line, two spaces deeper than the one around it. An element with a text and elements inside has its text right after its tag.

#parse

cromo
Element parse(string text)

The element at the top of the document in text, with all that is inside it. The declaration, the comments and the instructions are passed over, a CDATA is its text, and the entities are read: the five of XML and the characters by their number, as &#233;. Throws an Error with the line and the column for a text that is not well formed, as a tag that is not closed, and for an entity of a DTD, which is not known.