xml Bible for conversion for D

Adam Ruppe destructionator at gmail.com
Wed Oct 19 07:24:21 PDT 2011


I found std.xml useless too, so I wrote my own lib.

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff

Grab dom.d from there. First, convert your file to UTF-8. My lib
might work for you, but it assumes utf-8 so it will throw if it actually
encounters a non-ascii character from the 8859-1 charset.


Anyway the D code for dealing with that would look something like this:


import std.file;
import std.stdio;
import arsd.dom;

void main() {
    auto document = new Document(readText("bible.xml"));

    // the document is now the bible

    auto books = document.getElementsByTagName("b");
    foreach(book; books) {
       auto nameOfBook = b.n; // "Genesis" for example. All xml attributes are
available this same way

       auto chapters = book.getElementsByTagName("c");
       foreach(chapter; chapters) {
            auto verses = chapter.getElementsByTagName("v");
            foreach(verse; verses) {
                 auto v = verse.innerText;

                 // let's write out a passage
                 writeln(nameOfBook, " ", chapter.n, ":", verse.n, " ", v); //
prints "Genesis 1:1 In the beginning, God created [...]
            }
       }
    }
}


More information about the Digitalmars-d-learn mailing list