Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Tuesday, 4 December 2012

Writing and Reading to a XML File

Writing to a XML File

static void writeXML(Args _args)

{

XMLTextWriter xw; FileDemo ct;

;

xw = XMLTextWriter::newFile(“c:\\FileDemo.xml”);

xw.writeStartDocument();

xw.writeStartElement(“Details”);

while select ct

{

xw.writeStartElement(“CustomerDetails”);

xw.writeElementString(“Id”,ct.Id);

xw.writeElementString(“Name”,ct.Name);

xw.writeEndElement();

}

xw.writeEndElement();

xw.writeEndDocument();

xw.close();

}

Reading from a XML File

static void readingXML(Args _args)

{

XMLDocument xmlDoc = XMLDocument::newFile(“c:\\FileDemo.xml”);

int i, nooftags;

;

nooftags = xmlDoc.getElementsByTagName(“CustomerDetails”).length();

for(i=0; i<nooftags; i++)

{

info(strfmt(“%1″, xmlDoc.getElementsByTagName(“Id”).item(i).text()));

info(strfmt(“%1″, xmlDoc.getElementsByTagName(“Name”).item(i).text()));

}

}

Class2XML conversion

static void Class2XML(Args _args)


{


TreeNode    treeNode = TreeNode::findNode(@’\Classes\abValidation’);


TreeNode    childNode;


str nodename, code;


int  counter=1;


Boolean     foundObject;


XMLDocument xmlDoc;


XMLTextWriter xw;

;

xw = XMLTextWriter::newFile(“c:\\abValidation.xml”);

//pass treeNode.AOTname() name to create a xml with the class name xw.writeStartDocument();

if (treeNode)

{

xw.writeStartElement(treeNode.AOTname());

childNode = treeNode.AOTfirstChild();

while(counter <= treeNode.AOTchildNodeCount())

{

nodename = “”;

if(childNode)

{

nodename = childNode.treeNodeName();

if(childNode.treeNodeName())

{

xw.writeStartElement(nodename);

xw.writeElementString(“Data”,childNode.AOTgetSource());

xw.writeEndElement();

}

}

childNode = childNode.AOTnextSibling(); counter++;

}

xw.writeEndElement();

xw.writeEndDocument();

xw.close();

}

Box::info(“Xml Created Successfully”);

}

Xml2Class Conversion

static void Xml2Class(Args _args)

{

#define.paramsNode(“abValidation”)

XmlDocument xmlDoc;

XmlElement xmlRoot;

XmlElement xmlField;

XmlElement xmlRecord;

XmlNodeList xmlRecordList;

XmlNodeList xmlFieldList;

ClassBuild  classBuild;

DictClass   dictClass;

int i, j;

;

xmlDoc = new XmlDocument(); xmlDoc.load(@”c:\\abValidation.xml”); xmlRoot = xmlDoc.getNamedElement(#paramsNode); xmlRecordList = xmlRoot.childNodes(); classBuild = new ClassBuild(“abValidation_demo”, false);

for (i=0; i<xmlRecordList.length(); i++)

{

xmlRecord = xmlRecordList.item(i);

xmlFieldList = xmlRecord.childNodes();

for (j=0; j<xmlFieldList.length(); j++)

{

xmlField = xmlFieldList.item(j);

classBuild.addMethod(xmlRecord.baseName(),xmlField.innerText());

}

}

}