!--a11y-->
Exporting Context Data into XML 
In this article, we will utilize a special feature of Excel 2003 for directly importing an XML data file to be displayed as an XML list. Excel automatically derives an XML schema from the structure of the given document.
The structure of a simple XML data file looks like this:

The elements Quantity, Article, Color, PriceInEuro, and TotalPerArticleInEuro are children of the ProductsElement element, which itself is a child of the data root element Products. In other words, the descriptive elements such as Quantity and Color are nested inside the ProductsElement element, which is nested inside the data root element. In the absence of a style sheet, this is the layered structure of elements that Excel automatically interprets to bring the data into a worksheet:


In this article, we do not deal with the presentation of the exported table data in Excel based on XML style sheets, allowing us to control the placement and formatting of cells or the content of table header texts. The main focus of this article is to provide technical details about how to apply special Web Dynpro services and how to develop a reusable Web Dynpro component.
The custom code for creating an XML representation of context data is listed below. The consumer of the Excel export component passes references to a context node instance and to a map. This map contains the context attribute names and the corresponding header texts to be exported to the Excel file. Based on this approach, we can exclude some context attributes of special types (for example, binary for storing MIME objects), which are contained in a context node.
Component Controller ExcelExportComp.java in Component ExcelComp |
/* * The following code section can be used for any Java code that is * not to be visible to other controllers/views or that contains constructs * currently not supported directly by Web Dynpro (such as inner classes or * member variables etc.). </p> * * Note: The content of this section is in no way managed/controlled * by the Web Dynpro design time or the Web Dynpro runtime. */ //@@begin others
/** * Create an XML representation of the given context data. * RESTRICTION: Excel 2003 must be installed on the client machine for opening * the given xml data representation as an Excel file. * @return string representation of the created xml data for Excel 2003. */ private String toExcel(IWDNode dataNode, Map columnInfos) { StringBuffer x = new StringBuffer(); String attributeName, headerName; String entriesName = dataNode.getNodeInfo().getName(); String entryName = entriesName + "Element";
// trim given header texts, so that XML element names adhere to the // rule 'no spaces contained'. trimHeaderTexts(columnInfos);
x.append("<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"); x.append("<").append(entriesName).append(">\n");
for (int i = 0; i < dataNode.size(); ++i) { IWDNodeElement dataNodeElement = dataNode.getElementAt(i); x.append("<").append(entryName).append(">\n");
for (Iterator iter = columnInfos.keySet().iterator(); iter.hasNext();) { attributeName = (String) iter.next(); headerName = (String) columnInfos.get(attributeName); x .append("<") .append(headerName) .append(">") .append(dataNodeElement.getAttributeValue(attributeName)) .append("</") .append(headerName) .append(">\n"); }
x.append("</").append(entryName).append(">\n"); } x.append("</").append(entriesName).append(">\n"); return x.toString(); } … //@@end |
To obtain well-formed XML element names from the retrieved header texts, the sample application implements a simple trimming algorithm. It eliminates spaces and concatenates all substrings with an uppercase first letter. For example, the header text Product Price in EURO is trimmed to ProductPriceInEuro.
Component Controller ExcelExportComp.java in Component ExcelComp |
/** * Trims all given header texts because XML element names (to be created) must * adhere to XML rules (that is, they cannot include special characters, spaces, * and so on). */ private void trimHeaderTexts(Map columnInfos) { String attributeName, trimmedHeaderText; for (Iterator iter = columnInfos.keySet().iterator(); iter.hasNext();) { attributeName = (String) iter.next(); trimmedHeaderText = trimHeaderText((String) columnInfos.get(attributeName)); columnInfos.put(attributeName, trimmedHeaderText); } }
/** * Trims a single header text. Omit empty strings. Convert substrings to * lowercase. Convert first char of substrings to uppercase. * Example: "Product Price in EURO" -> "ProductPriceInEuro" */ private String trimHeaderText(String headerText) { StringBuffer newHeaderText = new StringBuffer(); String token; StringTokenizer tokenizer = new StringTokenizer(headerText.trim()); while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); newHeaderText.append(token.substring(0, 1).toUpperCase()); newHeaderText.append(token.substring(1).toLowerCase()); } return newHeaderText.toString(); } |
Utilizing the IWDCachedWebResource API