[xml] Newbie DOM Info



Hi All,

I posted a question asking for some DOM help ... but I didn't get what I
was looking for...

So I spent a day reading the docs and this is what I came up with...

If you interested in helping create a full blown DOM test harness email
me.


Here is the start of a DOM example...

//// xmltest.cpp 

#define IN_LIBXML
#include "libxml.h"

#define CHARCAST(x) reinterpret_cast<const unsigned char *>(x)

#include <string>
#include <fstream.h>

#include <string.h>

#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/SAX.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlerror.h>
#include <libxml/DOCBparser.h>
#include <libxml/entities.h>
#include <libxml/encoding.h>
#include <libxml/valid.h>
#include <libxml/xmlIO.h>
#include <libxml/uri.h>
#include <libxml/globals.h>



int test() 
{
        xmlDocPtr               doc;
        xmlNodePtr              cur;     
        xmlNodePtr              childNode;     
        std::string             buffer;
        xmlNodePtr              n;
    ofstream            fout;
        
        
        doc = xmlNewDoc(CHARCAST("1.0"));       
        
        n = xmlNewNode(NULL, CHARCAST("info"));         
        xmlDocSetRootElement(doc, n);

        cur = xmlDocGetRootElement(doc);

        
        xmlNewProp(n, CHARCAST("version"), CHARCAST("alpha"));
        xmlNewProp(n, CHARCAST("date"), CHARCAST("1/1/2003"));
        xmlNewProp(n, CHARCAST("color"), CHARCAST("red"));
        xmlNewProp(n, CHARCAST("number"), CHARCAST("\"&"));

        xmlNewTextChild (cur, NULL, CHARCAST("keyword"),
CHARCAST("keyword"));
        xmlNewTextChild (cur, NULL, CHARCAST("keyword"), CHARCAST("<"));
        xmlNewTextChild (cur, NULL, CHARCAST("keyword"), CHARCAST("&"));
        //childNode= xmlNewTextChild (cur, NULL, CHARCAST("keyword"),
CHARCAST(">"));

        childNode = xmlNewChild(cur, NULL, CHARCAST("list"), NULL);

        xmlNewTextChild (childNode, NULL, CHARCAST("name"),
CHARCAST("JG"));
        xmlNewTextChild (childNode, NULL, CHARCAST("name"),
CHARCAST("BG"));
        xmlNewTextChild (childNode, NULL, CHARCAST("name"),
CHARCAST("JAM"));
        xmlNewTextChild (childNode, NULL, CHARCAST("name"),
CHARCAST("SAM"));
        xmlNewTextChild (childNode, NULL, CHARCAST("name"),
CHARCAST("ALL"));

        xmlNewTextChild (cur, NULL, CHARCAST("keyword"), CHARCAST("&"));
        xmlNewTextChild (cur, NULL, CHARCAST("keyword"), CHARCAST("'"));
        xmlNewTextChild (cur, NULL, CHARCAST("keyword"),
CHARCAST("\""));
        xmlNewTextChild (cur, NULL, CHARCAST("keyword"),
CHARCAST("\n"));
        xmlNewTextChild (cur, NULL, CHARCAST("keyword"), CHARCAST("]"));
        xmlNewTextChild (cur, NULL, CHARCAST("keyword"),
CHARCAST("Testing 1 2 3 1 2 3 4 5 6 7 ! @ # $ % ^ & * ( ) : ; \" ' < > ,
. ? / testing"));

        std::string content;

        content = "LOTS OF CONTENT GOES HERE";
        xmlNewCDataBlock(doc, CHARCAST(content.c_str()),
content.length());
 

        xmlSaveFormatFile ("1.xml", doc, 1);
        xmlFreeDoc(doc);

    fout.close();

        //delete doc;

    return 0;
}


int main(int argc, char* argv[])
{
        test();

        return 0;
}


Tony Leotta
http://www.pillarsoftware.com/





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]