This blog post is dedicated to XML files. A simple example, showing you how to create an XML file. Luckily, Ax already contains the framework to create an XML file, so it can do most of our work.
static void XML_Basic(Args _args)
{ XMLDocument xmlDoc;
XMLNode nodeRoot, commentNode;
XMLNode PanelNode;
XMLNode LengthNode;
XMLNode WidthNode;
FileName xmlFileName;
;
// create XML
xmlDoc = XMLDocument::newBlank();
nodeRoot = xmlDoc.documentElement();
// create XML nodes
commentNode = xmlDoc.appendChild(xmlDoc.createComment('Comment'));
commentNode.text('XML file from http://dynamics-ax-live.blogspot.com');
PanelNode = xmlDoc.appendChild(xmlDoc.createElement('Panel'));
LengthNode = PanelNode.appendChild(xmlDoc.createElement('Length'));
LengthNode.text('2440');
WidthNode = PanelNode.appendChild(xmlDoc.createElement('Width'));
WidthNode.text('1220');
// write XML to file
xmlFileName=@'C:\MyXML.XML';
new FileIoPermission(xmlFileName, 'rw').assert();
xmlDoc.save(xmlFileName);
CodeAccessPermission::revertAssert();}
There are 3 basic steps in this small 'tutorial'. Step 1, create the XML document. Step 2, add the XML nodes. And step 3, save the XML document in a file. This example will create a very basic XML file, which introduces you to the world of tags, elements and attributes.
As XML files are a common way of exchanging information, you may want to get familiar to the terminology over here.
Monday, August 17, 2009
Subscribe to:
Post Comments (Atom)
Thanks. Please include the code to initialize 'xmlDoc' object.
ReplyDeletethanks i was looking for this .
ReplyDeletethe code xmldoc
xmlDoc = XMLDocument::newBlank();
hi , can you show us how to create namespace in the xml file?
ReplyDeleteHi there,
ReplyDeleteHow about using
xmlDoc.createAttribute("xmlns:xsi");
and applying the attribute to the XMLnode?
if anyone is useful even
ReplyDeleteXMLNode rootNode;
XMLNode NodeVend;
XmlElement element;
;
NodeVend = rootNode.appendChild(xmlDoc.createElement('Vendor'));
element = NodeVend;
element.setAttribute('VALOR', 'NIVEL');
thanks willy, and what if i want to create a namespcase like xmlns = "". the suggested method cannot accomplish this. thank you
ReplyDeleteHow to specify size of string
ReplyDeleteex:poNumber.innerText(_salesTable.PurchId);
_salesTable.purchid should only 20 charcaters
Nice post. You can visit following link to get Ax 2012 technical code and error solutions
ReplyDeleteAx 2012 Development and Coding
I need to add attributes to the root node of my XML and can't make it happen. Configuring the child nodes works fine, but my vendor's schema requires attributes on the root as well. I'm in AX4.0 (We're upgrading soon, but for now I'm stuck in 4.0) Any help would be appreciated
ReplyDeleteAlso, one of the root attributes needs to be named xsi:schemaLocation but the job strips off the xsi:
Deletecode:
node.setAttribute("xsi:schemaLocation","http://... ...file..xsd");
Hi Joe,
DeleteI also facing the same issue, if you had good luck please reply to my mail (Avivekananda@hsdyn.com)
This comment has been removed by the author.
Delete
DeleteWe were able to solve the issue of putting attributes on the root node of the XML. I've pasted sample code below as well as the resulting XML. I hope this helps.
XMLDocument doc;
XMLNode root;
XMLElement node,childNode;
XMLAttribute attr,attr2,attr3;
XMLNode XMLNode;
XMLNamedNodemap NodeMap;
doc = XMLDocument::newBlank();
// Create root node
root = doc.createElement("RootNodeName");
doc.appendChild(root);
// Add attributes to root
NodeMap = root.attributes();
attr = doc.createAttribute("xmlns:xsi");
attr.text("http://www.w3.org/2001/XMLSchema-instance");
nodeMap.setNamedItem(attr);
attr2 = doc.createAttribute("xmlns");
attr2.text("http://www.abc.123");
nodeMap.setNamedItem(attr2);
attr3 = doc.createAttribute3("xsi","schemaLocation","http://www.w3.org/2001/XMLSchema-instance");
attr3.text("http://www.abc.xyz fileName.xsd");
nodeMap.setNamedItem(attr3);
Results:
I need to add attributes to the root node of my XML and can't make it happen. Configuring the child nodes works fine, but my vendor's schema requires attributes on the root as well. I'm in AX4.0 (We're upgrading soon, but for now I'm stuck in 4.0) Any help would be appreciated
ReplyDeleteHi there,
ReplyDeleteIf somebody is interested about the issue with the ":" skipped in the attribut names I was able to solved it using for creating the attribute the method createattribute2.
For example
if you want to show in your xml:
xsi:schemaLocation('http://www.w3.org/2009/XmlSchema-instance')
use this line code:
XMLDocument1.createattribute2('schemaLocation','http://www.w3.org/2009/XmlSchema-instance')
Enjoy,
Anca