Monday, August 17, 2009

Create a XML file in Ax

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.

16 comments:

  1. Thanks. Please include the code to initialize 'xmlDoc' object.

    ReplyDelete
  2. thanks i was looking for this .

    the code xmldoc
    xmlDoc = XMLDocument::newBlank();

    ReplyDelete
  3. hi , can you show us how to create namespace in the xml file?

    ReplyDelete
  4. Hi there,

    How about using

    xmlDoc.createAttribute("xmlns:xsi");

    and applying the attribute to the XMLnode?

    ReplyDelete
  5. if anyone is useful even

    XMLNode rootNode;
    XMLNode NodeVend;
    XmlElement element;
    ;
    NodeVend = rootNode.appendChild(xmlDoc.createElement('Vendor'));
    element = NodeVend;
    element.setAttribute('VALOR', 'NIVEL');

    ReplyDelete
  6. thanks willy, and what if i want to create a namespcase like xmlns = "". the suggested method cannot accomplish this. thank you

    ReplyDelete
  7. How to specify size of string
    ex:poNumber.innerText(_salesTable.PurchId);
    _salesTable.purchid should only 20 charcaters

    ReplyDelete
  8. This is axapta ERP blog for Technical and functional fields and includes Microsoft Dynamics Axapta tutorials and Dynamics Axapta Coverage. This blog also contains x++ code help for Ax developer and solution of technical and functional daily issues. This blog is specific for Microsoft dynamics programming. Enterprise portal, SharePoint services, business connectors and Enterprise Resource Planning applications and sql database.It will help to get Microsoft Business Solutions.


    axapta ERP blog

    ReplyDelete
  9. Nice post. You can visit following link to get Ax 2012 technical code and error solutions

    Ax 2012 Development and Coding

    ReplyDelete
  10. 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

    ReplyDelete
    Replies
    1. Also, one of the root attributes needs to be named xsi:schemaLocation but the job strips off the xsi:

      code:
      node.setAttribute("xsi:schemaLocation","http://... ...file..xsd");

      Delete
    2. Hi Joe,

      I also facing the same issue, if you had good luck please reply to my mail (Avivekananda@hsdyn.com)

      Delete
    3. This comment has been removed by the author.

      Delete

    4. We 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:

      Delete
  11. 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

    ReplyDelete
  12. Hi there,

    If 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

    ReplyDelete