View source code
Display the source code in std/xml.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Module std.xml

Warning: This module is considered out-dated and not up to Phobos' current standards. It will remain until we have a suitable replacement, but be aware that it will not remain long term.

Classes and functions for creating and parsing XML

The basic architecture of this module is that there are standalone functions, classes for constructing an XML document from scratch (Tag, Element and Document), and also classes for parsing a pre-existing XML file (ElementParser and DocumentParser). The parsing classes may be used to build a Document, but that is not their primary purpose. The handling capabilities of DocumentParser and ElementParser are sufficiently customizable that you can make them do pretty much whatever you want.

Example

This example creates a DOM (Document Object Model) tree from an XML file.

import std.xml;
import std.stdio;
import std.string;
import std.file;

// books.xml is used in various samples throughout the Microsoft XML Core
// Services (MSXML) SDK.
//
// See http://msdn2.microsoft.com/en-us/library/ms762271(VS.85).aspx

void main()
{
    string s = cast(string) read("books.xml");

    // Check for well-formedness
    check(s);

    // Make a DOM tree
    auto doc = new Document(s);

    // Plain-print it
    writeln(doc);
}

Example

This example does much the same thing, except that the file is deconstructed and reconstructed by hand. This is more work, but the techniques involved offer vastly more power.

import std.xml;
import std.stdio;
import std.string;

struct Book
{
    string id;
    string author;
    string title;
    string genre;
    string price;
    string pubDate;
    string description;
}

void main()
{
    string s = cast(string) read("books.xml");

    // Check for well-formedness
    check(s);

    // Take it apart
    Book[] books;

    auto xml = new DocumentParser(s);
    xml.onStartTag["book"] = (ElementParser xml)
    {
        Book book;
        book.id = xml.tag.attr["id"];

        xml.onEndTag["author"]       = (in Element e) { book.author      = e.text(); };
        xml.onEndTag["title"]        = (in Element e) { book.title       = e.text(); };
        xml.onEndTag["genre"]        = (in Element e) { book.genre       = e.text(); };
        xml.onEndTag["price"]        = (in Element e) { book.price       = e.text(); };
        xml.onEndTag["publish-date"] = (in Element e) { book.pubDate     = e.text(); };
        xml.onEndTag["description"]  = (in Element e) { book.description = e.text(); };

        xml.parse();

        books ~= book;
    };
    xml.parse();

    // Put it back together again;
    auto doc = new Document(new Tag("catalog"));
    foreach (book;books)
    {
        auto element = new Element("book");
        element.tag.attr["id"] = book.id;

        element ~= new Element("author",      book.author);
        element ~= new Element("title",       book.title);
        element ~= new Element("genre",       book.genre);
        element ~= new Element("price",       book.price);
        element ~= new Element("publish-date",book.pubDate);
        element ~= new Element("description", book.description);

        doc ~= element;
    }

    // Pretty-print it
    writefln(join(doc.pretty(3),"\n"));
}

Functions

NameDescription
check(s) Check an entire XML document for well-formedness
decode(s, mode) Decodes a string by unescaping all predefined XML entities.
encode(s) Encodes a string by replacing all characters which need to be escaped with appropriate predefined XML entities.
isBaseChar(c) Returns true if the character is a base character according to the XML standard
isChar(c) Returns true if the character is a character according to the XML standard
isCombiningChar(c) Returns true if the character is a combining character according to the XML standard
isDigit(c) Returns true if the character is a digit according to the XML standard
isExtender(c) Returns true if the character is an extender according to the XML standard
isIdeographic(c) Returns true if the character is an ideographic character according to the XML standard
isLetter(c) Returns true if the character is a letter according to the XML standard
isSpace(c) Returns true if the character is whitespace according to the XML standard

Classes

NameDescription
CData Class representing a Character Data section
CDataException Thrown during CData constructor
CheckException Thrown during check()
Comment Class representing a comment
CommentException Thrown during Comment constructor
DecodeException Thrown during decode()
Document Class representing an XML document.
DocumentParser Class for parsing an XML Document.
Element Class representing an XML element.
ElementParser Class for parsing an XML element.
InvalidTypeException Thrown if comparing with wrong type
Item Abstract base class for XML items
PIException Thrown during ProcessingInstruction constructor
ProcessingInstruction Class representing a Processing Instruction section
Tag Class representing an XML tag.
TagException Thrown when parsing for Tags
Text Class representing a text (aka Parsed Character Data) section
TextException Thrown during Text constructor
XIException Thrown during XMLInstruction constructor
XMLException The base class for exceptions thrown by this module
XMLInstruction Class representing an XML Instruction section

Enums

NameDescription
DecodeMode Mode to use for decoding.
TagType Tag types.

Authors

Janice Caron

License

Boost License 1.0.