www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - xml Bible for conversion for D

reply Joel Christensen <joelcnz gmail.com> writes:
I've got xml text of a Bible version. I want to get the text in the 
following format:

class Bible {
   Book[] bs;
}

class Book {
   Chapter[] cs;
}

class Chapter {
   Verse[] vs;
}

class Verse {
   string v;
}

Here's a part of the xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bible>
<b n="Genesis">
<c n="1">
<v n="1">In the beginning, God created the heavens and the earth.</v>
<v n="2">The earth was without form and void, and darkness was over the 
face of the deep. And the Spirit of God

I would like any pointers on xml with D.

- Joelcnz
Oct 18 2011
next sibling parent reply Jesse Phillips <jessekphillips+d gmail.com> writes:
I suggest xmlp

http://www.dsource.org/projects/xmlp/

Example:

http://www.dsource.org/projects/xmlp/browser/trunk/test/books.d

He intends to push for it to be in Phobos so it uses the std namespace.

import std.xml2; 

On Wed, 19 Oct 2011 17:31:06 +1300, Joel Christensen wrote:

 I would like any pointers on xml with D.
 
 - Joelcnz

Oct 18 2011
next sibling parent reply Joel Christensen <joelcnz gmail.com> writes:
I think I want to stick with the current std xml library for now.

I think the books example is too different for me to work for what I want.
Oct 18 2011
parent Joel Christensen <joelcnz gmail.com> writes:
I don't care to learn xml at this stage. I just want to use it on that 
Bible file.

On 19-Oct-11 8:03 PM, Bernard Helyer wrote:
 I think I want to stick with the current std xml library for now.


Oct 19 2011
prev sibling next sibling parent Bernard Helyer <b.helyer gmail.com> writes:
On Wed, 19 Oct 2011 18:21:22 +1300, Joel Christensen wrote:

 I think I want to stick with the current std xml library for now.

No, you don't. It's a terrible library, held together by lashings of hate and the power of Satan* *only half kidding
Oct 19 2011
prev sibling next sibling parent Bernard Helyer <b.helyer gmail.com> writes:
On Wed, 19 Oct 2011 07:03:05 +0000, Bernard Helyer wrote:

 and the power of Satan*

Err, I can see how that could be construed as in poor taste, considering the subject matter of the XML document. I meen no offense. >_<
Oct 19 2011
prev sibling next sibling parent Bernard Helyer <b.helyer gmail.com> writes:
On Wed, 19 Oct 2011 07:04:53 +0000, Bernard Helyer wrote:

 meen

Just not my day, is it?
Oct 19 2011
prev sibling next sibling parent Bernard Helyer <b.helyer gmail.com> writes:
On Wed, 19 Oct 2011 21:45:29 +1300, Joel Christensen wrote:

 I don't care to learn xml at this stage. 

What do you mean by "learn xml"? I'm saying std.xml is a terrible library, and furthermore will be deprecated sooner or later. Use another library. There is already a suggestion in this thread, and I can give one too if you want. Friends don't let friends use std.xml.
Oct 19 2011
prev sibling parent Jesse Phillips <jessekphillips+d gmail.com> writes:
On Wed, 19 Oct 2011 18:21:22 +1300, Joel Christensen wrote:

 I think I want to stick with the current std xml library for now.
 
 I think the books example is too different for me to work for what I
 want.

std.xml has had many bugs, and hasn't had most of them fixed. It might work for your desire, but it is a bad idea to rely on such. xmlp is about the same as std.xml so you're going to have to learn something about parsing with delegates.
Oct 19 2011
prev sibling parent reply Adam Ruppe <destructionator gmail.com> writes:
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 [...]
            }
       }
    }
}
Oct 19 2011
parent Joel Christensen <joelcnz gmail.com> writes:
Thanks Adam. :-) It seems to be working now, with the stuff you gave me.

I was thinking of making a text file from it that my programs can load 
from. avoiding the xml file, so they load faster. But I think I'll have 
my program(s) use the xml file each time they're run, in the mean time.
Oct 19 2011