简体   繁体   中英

Excel to XML nested elements

I am trying to use Excel to generate an XML, Overview of XML in Excel & Export XML data

My XML looks something like this,more blocks and elements,this is just an example, :

<Block>
    <Element1>XXX</Element1>
    <Element2>YYY</Element2>
    <Element3>ZZZ</Element3>
    <Nested_Elements>
        <Nested_Element>AAA</Nested_Element>
        <Nested_Element>BBB</Nested_Element>
        <Nested_Element>CCC</Nested_Element>
    </Nested_Elements>
</Block>

in Excel I can map element1,element2 and element3 add diffrent values and export to XML,all works in a nice easy way,which is exactly why I used excel instead of writing a python or c# code,the problem is with Nested_Element1, Nested_Element2,Nested_Element3.

After alot of search I think there is no way to do nested elements directly with Excel,what would be the best intermidiate tool to use for this,My knowledge with Excel is not very good,so is VBA the only way to do it?

and if I use VBA will I have to create a code to write the whole XML file so cannot make use of the Excel feature Export XML?

Ok,it truned out very easy but I will post an answer here in case someone is still searching for this, 1- Map flat elements between excel cells with XML Map source. 2- VBA code to export the XML:

    'Export XMLMap from worksheet,this will export the flat Mapped elements
    Set Map = ActiveWorkbook.XmlMaps(1)
    Map.Export Url:=XmlFile, Overwrite:=True
    're-Load XML as DOM to process it
    Set XDoc = New MSXML2.DOMDocument
    XDoc.async = False: XDoc.validateOnParse = False
    XDoc.Load XmlFile

    'Update  XML with nested elements
    CellText = sheet_Board.Cells(22, "C").Value 'cell having nested elements values separated with "," ===> AAA,BBB,CCC
    Set Node = XDoc.SelectSingleNode("//Block")
    Set nodChild = XDoc.createElement("Nested_Elements")
    Result = Split(CellText, ",")
    For i = LBound(Result()) To UBound(Result())
        Set nodGrandChild = XDoc.createElement("Nested_Element")
        nodGrandChild.Text = Result(i)
        nodChild.appendChild nodGrandChild
    Next i
            
    Node.appendChild nodChild

This will create an XML with the added nested nodes having no indentation:

     <Block>
    <Element1>XXX</Element1>
    <Element2>YYY</Element2>
    <Element3>ZZZ</Element3>
    <Nested_Elements><Nested_Element>AAA</Nested_Element><Nested_Element>BBB</Nested_Element><Nested_Element>CCC</Nested_Element></Nested_Elements></Block>

To Fix the indentation add this piece of code (from stackoverflow https://stackoverflow.com/a/37634549/14360302 )

Set xslDoc = New MSXML2.DOMDocument
xslDoc.LoadXML "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" _
        & "<xsl:stylesheet version=" & Chr(34) & "1.0" & Chr(34) _
        & "                xmlns:xsl=" & Chr(34) & "http://www.w3.org/1999/XSL/Transform" & Chr(34) & ">" _
        & "  <xsl:strip-space elements=" & Chr(34) & "*" & Chr(34) & " />" _
        & "  <xsl:output method=" & Chr(34) & "xml" & Chr(34) & " indent=" & Chr(34) & "yes" & Chr(34) & "" _
        & "            encoding=" & Chr(34) & "UTF-8" & Chr(34) & "/>" _
        & "  <xsl:template match=" & Chr(34) & "node() | @*" & Chr(34) & ">" _
        & "    <xsl:copy>" _
        & "       <xsl:apply-templates select=" & Chr(34) & "node() | @*" & Chr(34) & " />" _
        & "    </xsl:copy>" _
        & "  </xsl:template>" _
        & "</xsl:stylesheet>"
xslDoc.async = False
Set XmlNewDoc = New MSXML2.DOMDocument
XDoc.transformNodeToObject xslDoc, XmlNewDoc   'Line to fix indention
XmlNewDoc.Save XmlFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM