表紙 編集 短縮
std.xml
XMLを生成・解析するためのクラス群と関数群です。
このモジュールの基本的な構造は、XML文書をゼロから生成する関数およびクラス群(Tag, Element and Document)と、既存のXMLファイルを解析するクラス群(ElementParser and DocumentParser)から成ります。解析用クラスでDocumentクラスの実体を生成することもあるでしょうが、本来の目的に見合った使い方ではありません。DocumentParserとElementParserの対応能力は広く、どんな要望にも合わせてカスタマイズすることができます。
Authors:
Janice Caron
Date:
2006.02.12
License:
Public Domain
Examples:
import std.xml;
import std.stdio;
import std.string;
// 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
struct Book
{
string id;
string author;
string title;
string genre;
string price;
string pubDate;
string description;
}
void main()
{
string s = import("books.xml");
// Check for well-formedness
check(s);
// Take it apart
Book[] books;
auto xml = new DocumentParser(s);
xml.onStartTag["book"] = delegate void(ElementParser xml)
{
Book book;
book.id = xml.tag.attr["id"];
xml.onEndTag["author"] = delegate void(in Element e) { book.author = e.text; };
xml.onEndTag["title"] = delegate void(in Element e) { book.title = e.text; };
xml.onEndTag["genre"] = delegate void(in Element e) { book.genre = e.text; };
xml.onEndTag["price"] = delegate void(in Element e) { book.price = e.text; };
xml.onEndTag["publish-date"] = delegate void(in Element e) { book.pubDate = e.text; };
xml.onEndTag["description"] = delegate void(in Element e) { book.description = e.text; };
xml.parse();
books ~= book;
};
xml.parse();
// Put it back together again;
auto doc = new Document("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;
}
// Now let's see pretty-print it to see what it looks like
writefln(join(doc.pretty(3),"\n"));
}
bool isChar(dchar c);
その文字がXML標準準拠の文字ならばtrueを返します
Standards:
XML 1.0 (http://www.w3.org/TR/1998/REC-xml-19980210)
std.utfにisValidDchar()があり、std.uniにisUniAlpha()などがすでにあるのに、なぜ別の文字テストが必要なのでしょうか。その応えは、それぞれが別の標準に準拠していて、境界的な部分では異なる結果を返すからです。たとえば、std.utf.isValidDchar('\u0008) はtrueを返す一方std.xml.isChar('\u0008')はfalseを返します。なぜならコントロールコードはUnicode文字としては有効ですが、XML文書では許されていないからです。似たような筋で、std.uni.isUniAlpha('\U00020000') はtrueを返しますが、std.xml.isLetter('\U00020000')はfalseを返します。なぜなら、XMLではUnified Han Ideographs(CJK統合漢字)はタグ名や属性名には許されていないからです。この関数は文字がW3Cコンソーシアムの規定するXMLで本当に許されているかどうかを調べるのに使います。
Params:
dchar c 調べる文字