Article ID 1520 Article Title HOWTO: Add formatting to an XML file? Article URL http://www.dataaccess.com/kbasepublic/KBPrint.asp?ArticleID=1520 KBase Category XML Date Created 01/23/2002 Last Edit Date 12/16/2013
Article Text
QUESTION:
When I create an XML file with Visual DataFlex XML DOM implementation, the data is written as one stream of XML data. My customer does have an XML reader which cannot handle this and wants me to put each element on a new line, preferably indented if the elements are nested. Without this my customer won't accept my programs.
ANSWER:
According to the XML rules it should not make any difference whether the data is supplied as one large string (stream of data).
The Microsoft DOM implementation does not have a property or method to add CR/LF and/or indent elements. There is, however, a solution possible through the usage of XMLTextNodes. These are nodes without a pair of tags. Normally, data is surrounded by an opening and closing tag (e.g. <customer>somedata</customer>). A TextNode is just the contents and no tagnames. If you put a CR/LF and/or TAB characters in a TextNode we will see formatted output in a texteditor or display to console.
NOTES:
- Reading an XML datafile with such textnodes into a DOM object removes the textnodes, since the Microsoft parser cannot detect the difference between a simple CR/LF and a real textnode. If it saw a CR/LF as textnode, you would not be able to import an XML file properly since formatting should not make a difference.
- Enumerating a node (element) with subnodes will show the TextNodes as nodes present. So, either add the formatting to the end of your XML adventure (just before saving) or be aware of the presence of these textnodes.
SAMPLES:
1. Add CR/LF and/or indent during the XML datatree construction
2. Add CR/LF and/or indent just prior to the save
KEYWORDS:
XML; XML Formatting; Line Breaks; Indenting; Linefeed; Carriage return; Textnode
Contributed By:
Vincent Oorsprong
Company: Data Access Worldwide
email: vincent.oorsprong@dataaccess.eu
Web Site: http://www.dataaccess.eu
Links Related to this Article
Exporting to XML with formatting after construction the XML DOM structure
Exporting to XML with formatting during XML DOM build
Email this Article
Email this Article to a Colleague
Send Feedback on this Article to Data Access Worldwide
Copyright ©2024 Data Access Corporation. All rights reserved.
The information provided in the Data Access Technical Knowledge Base is provided "as is" without warranty of any kind. Data Access Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Data Access Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Data Access Corporation or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion or limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.