XML and Excel

Pages in this article

  1. What is XML
  2. Characteristics of XML
  3. Structure of XML
  4. XML Schemas
  5. XML in Excel
  6. XML Validation
  7. Conclusion

XML Validation

Odd enough, validation of XML files against their attached schema definition is turned off by default. Turning on this option will force Excel to check each XML file against the schema during import and export.

First an XML file containing a reference to s schema must be mapped to Excel cells. Close all open files in Excel you don't need and select File, Open. Select file test2-en.xml. I've left out the name of the first employee from this file. Again, select the option "Use the XML source task pane". Drag the branch "employee" to any cell. Next, click the properties button on the table tools tab of the ribbon (External table Data group), See fig. 12.

XML properties button on ribbon
Figure 12: XML properties button on ribbon

For Excel 2003, you can get at the properties dialog by clicking the appropriate button on the "List" toolbar:

The Properties button on the List toolbar in Excel 2003
Figure 13: The Properties button on the List toolbar in Excel 2003

The XML properties dialog opens:

XML Map properties dialog
Figure 14: XML Map properties dialog

Check the box next to "Validate data against schema for import and export".

 Now click the refresh button to import the data from test2_en.xml. The following dialog appears:

Error message caused by importing an XML file that violates its schema
Figure 15: Error message caused by importing an XML file that violates its schema

By clicking the Details... button Excel shows you a bit more information about what is wrong with your XML file (Figure 16). In this case Excel indicates that it did not expect the element "street" to appear yet, but the element "name" (apologies for the partially Dutch screenshot, a bug in Excel's multi-language pack). This error message is caused by the fact that I omitted the name tag and hence this tag is not in the expected position in the XML file. Despite the error message Excel did import the data.

Details of the validation errors caused by the offending XML file
Figure 15: Details of the validation errors caused by the offending XML file

Validation also works when exporting data. Open the file ExcelXML_en.xlsx and remove the name column entirely. Now try to export the XML list (see figure 16)

Rightclick cell menu, XML option, export
Figure 16: Rightclick cell menu, XML option, export

Error message after exporting XML data that violates the schema.
Figure 17: Error message after exporting XML data that violates the schema.


 


Comments

Showing last 8 comments of 13 in total (Show All Comments):

 


Comment by: Jan Karel Pieterse (17-3-2011 09:10:52) deeplink to this comment

Hi Leisha,

I've seen it happen, but my memory fails me in why it occurs, sorry! Maybe it has to do with the structure of the map itself?


Comment by: Emma (16-12-2011 01:37:35) deeplink to this comment

how do i write a code behind a button in vba to export an data in an excel worksheet to an xml file. pls its very urgent


Comment by: Jan Karel Pieterse (16-12-2011 03:45:20) deeplink to this comment

Hi Emma,

If you insert a button from the Forms controls, you can attache a macro to it by right-clicking the button and selecting Assign Macro.

See www.jkp-ads.com/articles/controls.asp


Comment by: Emma (16-12-2011 09:42:10) deeplink to this comment

pls i need the code to put behind the button in excel
i need to export the data in excel to an xml file. The xml file is supposed to act as a database so subsequent excel data can also be saved in it


Comment by: Jan Karel Pieterse (18-12-2011 23:04:47) deeplink to this comment

Hi Emma,

The first thing I would do is to record a macro whilst manually importing the file. Then study that code and figure out where it needs to be modified to suit your needs.


Comment by: kpavan2004 (14-2-2014 21:10:51) deeplink to this comment

I have a file which contains 10 xml messages.Each XML message is in single line. I would like to import the xml file into excel for analysis. Please suggest the correct approach to import the 10 xml messages into excel


Comment by: Jan Karel Pieterse (17-2-2014 07:04:50) deeplink to this comment

Hi,

Have you tried importing the XML file already? What result do you get?


Comment by: alex (23-3-2014 09:42:34) deeplink to this comment

Using an xml tool would actually be a much easier way of validating your xml and schema.

See; http://www.liquid-technologies.com/FreeXmlTools/FreeXmlValidator.aspx


Have a question, comment or suggestion? Then please use this form.

If your question is not directly related to this web page, but rather a more general "How do I do this" Excel question, then I advise you to ask your question here: www.eileenslounge.com.




To post VBA code in your comment, use [VB] tags, like this: [VB]Code goes here[/VB].