Introduction to XML
XML stands for eXtensible Markup Language, and has generated significant interest ever since Microsoft’s decision to add XML support to Office applications. While HTML’s purpose is for displaying data, XML’s more relates to carrying data. Unlike the pre-defined tags in HTML, the user defines the tags for HTML and source files are stored in text files.
*Note that tag and element are the same thing
XML & Excel
Users can save a spreadsheet as an XML document, as well as open XML documents as new spreadsheets. Moreover, data can also be imported into a worksheet from an XML document. The majority of cross XML-Excel, XML-VBA applications are only supported from Excel 2003/2007 onwards.
Requirements of an XML File
- XML declaration – defines XML version and character encoding of the document. Always the first line of code, or else Excel won’t be able to recognize the file as XML (will see it as a text file instead)
- Root element – pair of tags that all other tags are nested within (aka. child elements)
- All tags must be in pairs (ie) have an accompanying closing tag
- Tags are case-sensitive (ex) <TEST> </test> do not work but <TEST> </TEST> work. It is XML convention to name tags using lowercases and underscores
- Attributes must be in quotation marks – attributes are names that can be assigned to pairs. Try using child elements over attributes
<?xml version="1.0" encoding="UTF-8"> 'xml declaration <test> 'root element <problem> 'child element <number>1</number> <left_operand>2</left_operand> <operator>+</operator> <right_operand>1</right_operand> <answer>3</answer> </problem> <img src="questionmark.jpg" /> 'closing slash, even though HTML-version has no closing tag <problem problemID="Hard Level"> 'tag with name <number>2</number> <left_operand>3</left_operand> <operator>+</operator> <right_operand>2</right_operand> <answer>5</answer> </problem> 'child element </test> 'root element