How to add attributes to xml file

Kisu
Member Posts: 381
I'm trying to export xml file with some namespacess
I already build xml port that expots the data, I only need the namespacess attributes to the first element
Here is some of the code:
IF ISSERVICETIER THEN BEGIN
CREATE(XMLDOMDocument, FALSE, TRUE);
END;
XMLDOMDocument.async(FALSE);
XMLDOMDocument.load(XMLstream);
XMLDOMCurrNode := XMLDOMDocument.selectSingleNode('analysis'); // This should select the first element
// Here I'd need to put the attributes for the element
XMLDOMDocument.save('process.xml');
I allready tried to use 'Microsoft XML, v6.0'.IXMLDOMAttribute for adding the element attribute but somehow it does not work, I'm not sure how to use it properly
I already build xml port that expots the data, I only need the namespacess attributes to the first element
Here is some of the code:
IF ISSERVICETIER THEN BEGIN
CREATE(XMLDOMDocument, FALSE, TRUE);
END;
XMLDOMDocument.async(FALSE);
XMLDOMDocument.load(XMLstream);
XMLDOMCurrNode := XMLDOMDocument.selectSingleNode('analysis'); // This should select the first element
// Here I'd need to put the attributes for the element
XMLDOMDocument.save('process.xml');
I allready tried to use 'Microsoft XML, v6.0'.IXMLDOMAttribute for adding the element attribute but somehow it does not work, I'm not sure how to use it properly

K.S.
0
Answers
-
It should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<analysis version="String" xsi:noNamespaceSchemaLocation="analysis.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<currentDate>1967-08-13</currentDate>
<supplier>
<supplierId>String</supplierId>
...
And it looks like this currently
<?xml version="1.0" encoding="UTF-8"?>
<analysis version="String">
<currentDate>1967-08-13</currentDate>
<supplier>
<supplierId>String</supplierId>
...K.S.0 -
I solved it, after painfull 5hours of testing stuff.. but its done.
I thought I'd share this as I spend hours and hours looking for reference and found no help/example, I hope its usefull for others too:
// ++004, 30.10.2013 Adds namespaces, Nav 2013
{ Vars: (It should work on older versions of Microsoft XML)
Name DataType Subtype Length
XMLDOMNode Automation 'Microsoft XML, v6.0'.IXMLDOMNode
XMLDOMAttribute Automation 'Microsoft XML, v6.0'.IXMLDOMAttribute
XMLDOMDocument Automation 'Microsoft XML, v6.0'.DOMDocument
setupTable Record The source where filename is given
}
// Automation initializations
IF ISSERVICETIER THEN BEGIN
CREATE(XMLDOMDocument, FALSE, TRUE); // Note this changed from 2009
END;
XMLDOMDocument.async(FALSE);
// Load the file into memory
XMLDOMDocument.load(setupTable.filename); // Load to XML DOM
// Remove the exported file
ERASE(setupTable.filename);
// Element which to add the attributes (this sets the Node/Element which attribute to change)
XMLDOMNode := XMLDOMDocument.selectSingleNode('analysis'); // <-- analysis is the main XML element of the file
// First attribute
XMLDOMAttribute := XMLDOMNode.ownerDocument.createAttribute('xsi:noNamespaceSchemaLocation');
XMLDOMAttribute.nodeValue := 'analysis.xsd';
XMLDOMNode.attributes.setNamedItem(XMLDOMAttribute);
// Second attribute
XMLDOMAttribute := XMLDOMNode.ownerDocument.createAttribute('xmlns:xsi');
XMLDOMAttribute.nodeValue := 'http://www.w3.org/2001/XMLSchema-instance';
XMLDOMNode.attributes.setNamedItem(XMLDOMAttribute);
// Save to a File again
XMLDOMDocument.save(setupTable.filename);
// --004K.S.0 -
Working with this I also noticed that the tag names and attributes are case sensitive.
One falty letter and the debug returns that the automation has not been initialized (which was a bit odd error for this imo)
Sometimes I wonder why dont people use standards that have been given to them :-kK.S.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions