Results 1 to 6 of 6

Thread: [Excel] XML Attribute Duplicated

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    [Excel] XML Attribute Duplicated

    Im encountering a strange issue where I am not adding an attribute to my XML tag but it is appearing anyway.
    vb Code:
    1. Option Explicit
    2. ' This procedure creates XML document
    3. ' Requires msxml.dll (Go to Project --> References and
    4. ' and choose Microsoft XML version 2.0, or whatever the
    5. ' current version you have installed)
    6.  
    7.  
    8. Private Sub Create_XML()
    9.    
    10.    Dim objDom As DOMDocument
    11.    Dim objProcInst As IXMLDOMProcessingInstruction
    12.    Dim objRootElem As IXMLDOMElement
    13.    Dim objHeaderElem As IXMLDOMElement
    14.    Dim objTargetElem As IXMLDOMElement
    15.    Dim objTradePosElem As IXMLDOMElement
    16.    Dim objMemberElem As IXMLDOMElement
    17.    Dim objMemberRel As IXMLDOMAttribute
    18.    Dim objMemberName As IXMLDOMElement
    19.    Dim objAtt As IXMLDOMAttribute
    20.    Set objDom = New DOMDocument
    21.    
    22.  
    23.  
    24.     Set objProcInst = objDom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8' standalone='yes'")
    25.     objDom.appendChild objProcInst
    26.    
    27.    ' Creates root element
    28.    Set objRootElem = objDom.createElement("riskImport")
    29.    objDom.appendChild objRootElem
    30.    
    31.    ' Creates NameSpace Attributes etc on to the root Element
    32.    Set objAtt = objDom.createAttribute("xmlns")
    33.    objAtt.NodeValue = "http://www.test.com/xml"
    34.    objRootElem.setAttributeNode objAtt
    35.    
    36.    Set objAtt = objDom.createAttribute("schemaVersion")
    37.    objAtt.NodeValue = "1.0"
    38.    objRootElem.setAttributeNode objAtt
    39.    
    40.     Set objAtt = objDom.createAttribute("importDatetime")
    41.    objAtt.NodeValue = "2009-06-05T14:20:20.661Z"
    42.    objRootElem.setAttributeNode objAtt
    43.  
    44.    
    45.    ' Create riskDataHeader element
    46.    Set objHeaderElem = objDom.createElement("riskDataHeader")
    47.    objRootElem.appendChild objHeaderElem
    48.  
    49.  
    50.    Set objMemberName = objDom.createElement("portfolio")
    51.    objMemberName.Text = "myPortfolio"
    52.    objHeaderElem.appendChild objMemberName
    53.    
    54.    Debug.Print objDom.XML
    55.    
    56. End Sub
    This outputs the following.
    Why is the xmlns attribute appearing on the riskDataHeader element and how to I prevent it from happening?
    Code:
    <?xml version="1.0" standalone="yes"?>
    <riskImport xmlns="http://www.test.com/xml" schemaVersion="1.0" importDatetime="2009-06-05T14:20:20.661Z">
    	<riskDataHeader xmlns="">
    		<portfolio>myPortfolio</portfolio>
    	</riskDataHeader>
    </riskImport>

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Excel] XML Attribute Duplicated

    ' Creates NameSpace Attributes etc on to the root Element
    Set objAtt = objDom.createAttribute("xmlns")
    objAtt.NodeValue = "http://www.test.com/xml"
    objRootElem.setAttributeNode objAtt
    you create it??

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Re: [Excel] XML Attribute Duplicated

    Isn't that just adding the attribute on the root element though?
    Code:
    <riskImport xmlns="http://www.test.com/xml"  schemaVersion="1.0" importDatet.....
    The problem I have is that it also appears as an attribute on EVERY element below the root as well.

    What is wierder is that just before I save the file at the end, if I output objHeaderElem.xml to the immediate window it doesn't contain this attribute, but if I output objRootElem.xml to the immediate, the header info DOES include it!!

    Code:
    ?objHeaderElem.xml
    <riskDataHeader><portfolio>myPortfolio</portfolio></riskDataHeader>
    
    ?objRootElem.xml
    <riskImport xmlns="http://www.test.com/xml" schemaVersion="1.0" importDatetime="2009-06-05T14:20:20.661Z">
    <riskDataHeader xmlns=""><portfolio>myPortfolio</portfolio></riskDataHeader></riskImport>

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Excel] XML Attribute Duplicated

    Could it be that as it is a header it is added afterwards (like headers n footers in word) to the data.

    So you define the headers as things you want to add to all data elements on save?

    (I am guessing tho)

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Re: [Excel] XML Attribute Duplicated

    Hmmm, I am still not sure what is causing it.
    However, I have realised that I don't have to create an attribute before setting the value.
    The setAttribute seems to create one.
    So now I am doing the following instead
    vb Code:
    1. objRootElem.setAttribute "xmlns", "http://www.test.com/xml"

    Wierd.

  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: [Excel] XML Attribute Duplicated

    very

    possibly the create attribute means that the current and next child use it?

    DOM
    --Riskimport
    --+xmlns
    --+schemaversion
    --+importdate
    ----RiskDataHeader
    ------Portfolio

    In theory tho if it grabbed the nodes from the parent then the portfolio would also have one. The only thing I can see different is that you set a value in portfolio... could it be that it defaultly adds in nodes?

    weird how it doesn't add all the nodes tho.

    at least you found a way around it.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width