How to read an Xml file and write a new file with a dialog

static void XMLTest(Args _args)
{
    str             filename, nodeName,txtFile,strTxtRow;
    str             AttName;
    int             i,j,k;
    boolean         Valid;
    XMLDocument XMLdoc = new XMLDocument();
    Dialog dialog = new Dialog("Enter File Name Complete Path");
    DialogField xmlfile;
    XMLNode         rootNode, bodyNode,bodyNode1,AttNode;
    XMLNodeList     bodyLines,childLine;
    XMLElement      xmlElement;
    XMLNodeList     _xmlNodelist;
    commaIO         myfile;
    ;
   
    xmlfile  = dialog.addField(extendedTypeStr(filenameopen),"XML File Name");
    dialog.filenameLookupFileName("*.xml");
    if (dialog.run())
    {
        if(xmlfile.value() == "")
        {
            throw warning (strfmt("@SYS26332","XML Name"));
        }
        else
        {
            filename = xmlfile.value();
            Valid    = true;
        }
    }
    dialog.close();
   
    if (Valid)
    {
        XMLdoc.load(filename);
        if(!xmldoc.load(filename))
        {
            box::info("Can't load xml document");
        }
   
        txtFile = "C:\\Tmp\\XmlData.txt";
        myfile = new commaIO(txtFile,"w");
   
        rootNode = xmldoc.documentElement();
        nodeName = rootnode.baseName();
        xmlElement = xmldoc.documentElement();
        if(rootnode.hasChildNodes())
        {
            bodyLines = rootnode.childNodes();
   
            for(j = 0; j< bodyLines.length(); j++)
            {
                bodyNode = bodyLines.nextNode();
                strTxtRow = bodyNode.baseName();
                myfile.write(strTxtRow);
                if(bodyNode.hasChildNodes())
                {
                    childLine = bodyNode.childNodes();
                    for(k= 0; k<childLine.length(); k++)
                    {
                        strTxtRow = "";
                        bodyNode1 = childLine.item(k);
                        nodeName = bodyNode1.baseName();
                        AttNode = xmlElement.getAttributeNode(nodeName);
                        strTxtRow = nodeName;
                        _xmlNodelist = bodyNode.selectNodes(nodeName);
                        for(i= 0;i< _xmlNodelist.length();i++)
                        {
                            strTxtRow = strTxtRow + ': ' +
                            _xmlNodelist.item(i).text();
                        }
                            bodyNode.selectSingleNode(nodeName).text();
                        myfile.write(strTxtRow);
                    }
                }
            }
        }
        box::info("Records are Imported to XmlData.txt File at C\\Tmp\\:","Import Operation","XML to Axapta");
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.