Class representing a Character Data section
Thrown during CData constructor
Thrown during check()
Class representing a comment
Thrown during Comment constructor
Thrown during decode()
Class representing an XML document.
Class for parsing an XML Document.
Class representing an XML element.
Class for parsing an XML element.
Thrown if comparing with wrong type
Abstract base class for XML items
Thrown during ProcessingInstruction constructor
Class representing a Processing Instruction section
Class representing an XML tag.
Thrown when parsing for Tags
Class representing a text (aka Parsed Character Data) section
Thrown during Text constructor
Thrown during XMLInstruction constructor
The base class for exceptions thrown by this module
Class representing an XML Instruction section
Mode to use for decoding.
Tag types.
Check an entire XML document for well-formedness
Decodes a string by unescaping all predefined XML entities.
Encodes a string by replacing all characters which need to be escaped with appropriate predefined XML entities.
Returns true if the character is a base character according to the XML standard
Returns true if the character is a character according to the XML standard
Returns true if the character is a combining character according to the XML standard
Returns true if the character is a digit according to the XML standard
Returns true if the character is an extender according to the XML standard
Returns true if the character is an ideographic character according to the XML standard
Returns true if the character is a letter according to the XML standard
Returns true if the character is whitespace according to the XML standard
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) std.file.read("books.xml"); // Check for well-formedness check(s); // Make a DOM tree auto doc = new Document(s); // Plain-print it writeln(doc); }
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.
1 import std.xml; 2 import std.stdio; 3 import std.string; 4 5 struct Book 6 { 7 string id; 8 string author; 9 string title; 10 string genre; 11 string price; 12 string pubDate; 13 string description; 14 } 15 16 void main() 17 { 18 string s = cast(string) std.file.read("books.xml"); 19 20 // Check for well-formedness 21 check(s); 22 23 // Take it apart 24 Book[] books; 25 26 auto xml = new DocumentParser(s); 27 xml.onStartTag["book"] = (ElementParser xml) 28 { 29 Book book; 30 book.id = xml.tag.attr["id"]; 31 32 xml.onEndTag["author"] = (in Element e) { book.author = e.text(); }; 33 xml.onEndTag["title"] = (in Element e) { book.title = e.text(); }; 34 xml.onEndTag["genre"] = (in Element e) { book.genre = e.text(); }; 35 xml.onEndTag["price"] = (in Element e) { book.price = e.text(); }; 36 xml.onEndTag["publish-date"] = (in Element e) { book.pubDate = e.text(); }; 37 xml.onEndTag["description"] = (in Element e) { book.description = e.text(); }; 38 39 xml.parse(); 40 41 books ~= book; 42 }; 43 xml.parse(); 44 45 // Put it back together again; 46 auto doc = new Document(new Tag("catalog")); 47 foreach (book;books) 48 { 49 auto element = new Element("book"); 50 element.tag.attr["id"] = book.id; 51 52 element ~= new Element("author", book.author); 53 element ~= new Element("title", book.title); 54 element ~= new Element("genre", book.genre); 55 element ~= new Element("price", book.price); 56 element ~= new Element("publish-date",book.pubDate); 57 element ~= new Element("description", book.description); 58 59 doc ~= element; 60 } 61 62 // Pretty-print it 63 writefln(join(doc.pretty(3),"\n")); 64 }
Copyright Janice Caron 2008 - 2009.
Warning: This module is considered out-dated and not up to Phobos' current standards. It will be removed from Phobos in 2.101.0. If you still need it, go to https://github.com/DigitalMars/undeaD
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 <i>may</i> 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.