Writing an XML file based on raw data from an Excel 2010 worksheet

jmohsin
jmohsin used Ask the Experts™
on
1. I have an Excel 2010 file with 20+ columns.

2. I need to write an XML (output) file which is based on the data in Excel 2010 (input).

3. The SAME (selected) columns from EACH row provide blocks for the XML file. The XML file is also supposed to comply to an XSD.

4. The FIRST row in the Excel sheet contains the XML tag names.

5. Each subsequent row in Excel 2010 turns into a series of tagged entries in the XML file.

6. Is this even possible in Excel 2010? I did see "XML Maps" in Excel 2010; I have never used XML Maps before, hence this question.

7. Please post high level steps, or pointer to detailed Excel documentation.

8. If you have a sample Excel file that you turned into XML, that is an A grade response :-).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The attached file, I create an XML file....

I am not looping through existing sheets to get the data... but am thinking your main question is how to create the xml file.

The macro in the attached sheet will create an xml file with the following data:

<OuterMostNode>
  <NextLevelOutSection>
    <NestedField>NestedFieldData</NestedField>
  </NextLevelOutSection>
  <Field1>Field1Data</Field1>
  <Field2>Field2Data</Field2>
  <Field2>Field3Data</Field2>
</OuterMostNode>

I used constants to populate the data... but you should be able to leverage the routine to build whatever XML file you want.

To use this code you need to make sure that from within Visual Basic  you select tools/References and select Microsoft XML, v6.0

(it is already selected in the attached sheet).

NOTE: the result of the macro will write all of the xml text on a single line... (without carriage returns)... for the indenting displayed above... I reformatted with PSPad, a free editor avaialble online.  XMLPad (Free) / XMLSpy (Expensive) have similar capabilities.
writeXML.xlsm
If you already have an XSD file, then all you need to do is open your Excel data file and map the fields, then export to XML. Excel handles the rest.

To map the fields, open the Developer tab and click the Source button in the XML section to open the XML Source window. Click the "XML Maps..." button at the bottom of the window. Click the "Add..." button to open a browsing window, and find your XSD file. Click Open and OK to confirm and close back out to the main Excel window. The XML Source window should now contain your list of fields. Drag and drop each field name onto the corresponding column to map it. Then go back to the Developer tab and click Export to open a browsing window to save the XML version of the data.

If you need to create an XSD, I recommend the XML Tools Add-In available from Microsoft here:
http://office.microsoft.com/en-us/excel-help/create-an-xml-data-file-and-xml-schema-file-from-worksheet-data-HA010263509.aspx#BM1

See the attached for a small example, with the original data, the XSD, the data with the XSD mapped, and the exported XML.
Weather-Table---Raw.xlsx
WeatherTable.XSD
Weather-Table---XML-Mapped.xlsx
Weather-Table.xml

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial