www.digitalmars.com         C & C++   DMDScript  

D - Which XML parser?

reply Ant <Ant_member pathlink.com> writes:
Which D XML parser are you using?

I downloaded Andy's xmld but he said that
we have an expat port available and should
be better.
Couldn't find it.

Thanks
Ant
Oct 06 2003
next sibling parent reply Brad Anderson <brad sankaty.com> writes:
Ant wrote:
 Which D XML parser are you using?
 
 I downloaded Andy's xmld but he said that
 we have an expat port available and should
 be better.
 Couldn't find it.
 
 Thanks
 Ant
 
 
Ant, I have attempted to wrap 1.95.3 of the expat parser, with very little luck. I'm afraid I'm only a beginner C programmer, and don't totally understand how a lot of things work, especially with callback functions and function pointers, etc. I've attached my expat.d wrapper module, so maybe you can take a look and see what I'm doing wrong. Also, I had to run implib on the precompiled dll's that came with the Win version of expat-1.95.3. I think I might be pretty close. You may want to try extern(Windows) instead of extern(C) in the beginning of expat.d, although I'm not sure of the difference and Walter or the docs had said at one time to use extern (C) on the whole wrapper module. With extern (Windows), it compiles, but then can't find the dll. I'll attach my whole project for you. Unzip by using path/folder info. The \ref directory has the originals. Let me know how you do, and if you succeed, what you did. Hope this helps...
Oct 08 2003
parent reply Ant <Ant_member pathlink.com> writes:
In article <bm21ua$ng0$1 digitaldaemon.com>, Brad Anderson says...
This is a multi-part message in MIME format.
--------------040803070706080207070403
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Ant wrote:
 Which D XML parser are you using?
 
 I downloaded Andy's xmld but he said that
 we have an expat port available and should
 be better.
I have attempted to wrap 1.95.3 of the expat parser, with very little luck. I'm afraid I'm only a beginner C programmer, and don't totally understand how a lot of things work, especially with callback functions and function pointers, etc. I've attached my expat.d wrapper module, so maybe you can take a look
I'm bookmarking this. If nobody else does I might take a look in a couple of days. (the best I can do is to give you back a linux version) Keep us informed if you have any progress. Thanks Ant
Oct 08 2003
parent Brad Anderson <brad sankaty.com> writes:
 I'm bookmarking this.
 If nobody else does I might take a look in a couple of days.
 (the best I can do is to give you back a linux version)
 Keep us informed if you have any progress.
 
Linux version is fine. Also, if we make progress, I'll wrap 1.95.6, the latest release, January 2003, and attempt it for Win32 as well. I've also been looking at the Java version of Xerces, which has a SAX and a DOM parser as well as XML 1.1 support. I'd be much better at converting Java to D, and I may toy with that in the future. Brad
Oct 09 2003
prev sibling parent Mike Hearn <mike theoretic.com> writes:
I did an experimental wrapping of the libxml2 XmlReader API one night, to
get a feel for how hard it would be:

/* -*- mode: c++-mode; c-basic-offset: 4 -*-
 *
 * Example of using the libxml2 xmlReader API from D
 * (c) 2003 Mike Hearn <mike theoretic.com>
 * Licensed under the GPL
 */

import string;

typedef void* xmlTextReaderPtr;

enum ReaderNodeType {
    Element = 1,
    Attribute =	2,
    Text = 3,
    CDATA = 4,
    EntityRef =	5,
    Entity =		6,
    ProcessingInstruction = 7,
    Comment = 8,
    Document = 9,
    DocumentType = 10,
    DocumentFrag = 11,
    Notation = 12,
    ElementEnd = 15
}

extern (C) {
    xmlTextReaderPtr xmlNewTextReaderFilename(char *URI);
    void xmlFreeTextReader(xmlTextReaderPtr reader);

    /* iterators */
    int	xmlTextReaderRead(xmlTextReaderPtr reader);

    /* attributes */
    int		xmlTextReaderAttributeCount(xmlTextReaderPtr reader);
    char       *xmlTextReaderLocalName	(xmlTextReaderPtr reader);
    char       *xmlTextReaderName	(xmlTextReaderPtr reader);
    int		xmlTextReaderNodeType	(xmlTextReaderPtr reader);
    char       *xmlTextReaderPrefix	(xmlTextReaderPtr reader);
    char       *xmlTextReaderValue	(xmlTextReaderPtr reader);
    char       *xmlTextReaderXmlLang	(xmlTextReaderPtr reader);
    int		xmlTextReaderReadState	(xmlTextReaderPtr reader);

    char       *xmlTextReaderGetAttribute(xmlTextReaderPtr reader, char *name);
}

class Reader {
 private:
    xmlTextReaderPtr _reader;
 public:
    this(char[] filename) {
	_reader = xmlNewTextReaderFilename(string.toStringz(filename));
    }
    
    ~this() {
	assert(_reader);
	xmlFreeTextReader(_reader);
    }

    int read() {
	return xmlTextReaderRead(_reader);
    }

    void process() {
	int ret;
	ret = read();
	while (ret == 1) {
	    callback(this);
	    ret = read();
	}
    }
    
    void delegate(Reader reader) callback;

    char[] name() { return string.toString(xmlTextReaderName(_reader)); }
    ReaderNodeType type() { return (ReaderNodeType)
xmlTextReaderNodeType(_reader); }
    char[] value() { return string.toString(xmlTextReaderValue(_reader)); }
}

int main(char[][] args) {
    printf("\nXML Demo\n\n");

    
    void cb(Reader reader) {
	/* This function will be called as each node is read */
	
        switch ( (int)reader.type ) {
	    /* slight language wart here, enums should be acceptable in a switch
expression imho */
	    
	    case ReaderNodeType.Element: printf("start: <%.*s>\n", reader.name); break;
	    case ReaderNodeType.Text: printf("text :   %.*s\n", reader.value); break;
	    case ReaderNodeType.ElementEnd: printf("end  : </%.*s>\n", reader.name);
break;
	    default: printf("\n"); break;
		
	}

    }

    Reader reader2 = new Reader("test.xml");    
    reader2.callback = &cb;
    reader2.process();
    
    return 0;
}

.... you get the idea. Obviously it's not a
full DOM but the XmlReader API is quite easy to use anyway.

thanks -mike
Oct 14 2003