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.
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 & ink</item></order>The guide: JSON.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Attribute
struct AttributeAn 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
struct ElementAn 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 orderstring text: the text right inside it, with the entities readlist<Element> children: the elements inside it, in their order
#Element.localName
string localName() constThe name without its prefix: "Body" for "s:Body".
#Element.attribute
string attribute(string key) constThe value of the attribute key, or "" when the element has none.
#Element.hasAttribute
bool hasAttribute(string key) constWhether the element has the attribute key.
#Element.setAttribute
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
bool has(string key) constWhether an element named key is right inside this one.
#Element.child
Element child(string key) constThe first element named key right inside this one, or an element without a name when there is none.
#Element.textOf
string textOf(string key) constThe 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
list<Element> all(string key) constThe elements named key right inside this one, in their order.
#Element.add
void add(move Element inside)Puts an element inside this one, after the others.
#Element.addText
void addText(string key, string content)Puts inside this one an element named key with a text: part.addText("ETag", tag) for <ETag>...</ETag>.
#Funzioni
#element
Element element(string name)An element named name, with nothing in it, to fill and to write.
#escape
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 < b".
#encode
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
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
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 é. 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.