XML Parsing

Iain staffell at gmail.com
Fri May 18 17:00:49 PDT 2012


On Friday, 18 May 2012 at 23:31:05 UTC, Iain wrote:
> Aah thank you!  Finally, an XML parser that works in D!!!

Adam, thanks for this!  I guess you don't need much documentation 
for your code, as you can just look up the wealth of tutorials 
that have been written for Javascript's XML parser.

I have re-jigged one of std.xml's examples as follows - and it 
works!

If there were a vote (and there probably should be) I would 
suggest your code ought to replace std.xml.  How can D be taken 
seriously when it has major parts of the standard library broken?



/*
  *  read all the titles from book.xml
  *
  *  uses dom.d and characterencodings.d by alex d ruppe:
  *  
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff
  */

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

void main()
{
	// http://msdn2.microsoft.com/en-us/library/ms762271(VS.85).aspx
	auto document = new Document(readText("book.xml"), true, true);

	auto map = document.requireSelector("catalog");

	foreach (book; document.getElementsByTagName("book"))
	{
		string title = 
book.getElementsByTagName("title")[0].innerText();
		
		writeln(title);
	}
}







More information about the Digitalmars-d-learn mailing list