How to add attributes to xml file

KisuKisu 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 :(
K.S.

Answers

  • KisuKisu Member Posts: 381
    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"&gt;
    <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.
  • KisuKisu Member Posts: 381
    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);


    // --004
    K.S.
  • KisuKisu Member Posts: 381
    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 :-k
    K.S.
Sign In or Register to comment.