|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * XEDOMELEMENTS.CPP 00003 * Copyright (c) 2007 Nokia Corporation. 00004 * This material, including documentation and any related computer 00005 * programs,is protected by copyright controlled by Nokia Corporation. 00006 */ 00007 00008 #include <e32base.h> 00009 #include "XeDomExample.h" 00010 00011 00012 /******************************************************** 00013 * This example deals with element class of XML engine DOM 00014 * It shows adding of new elements with/without namespaces, 00015 * creating elements with auto prefixes, removal of element 00016 * nodes, setting content text for elements (both raw and 00017 * escaped), fetching elements by their name, removing NS 00018 * declarations of an elements etc. 00019 *********************************************************/ 00020 EXPORT_C void CXEDomEx::ElementsExampleL() 00021 { 00022 // some examples of adding and removing elements 00023 AddRemoveElementsL(); 00024 00025 // accessing element text - subexample 00026 SetElementTextL(); 00027 } 00028 00029 00030 void CXEDomEx::AddRemoveElementsL() 00031 { 00032 RXmlEngDocument document; 00033 TBuf<KFilePathSize> filepath(iDrive); 00034 _LIT(KFilename, "\\xedomexample\\input\\XML_nsprocessing.xml"); 00035 filepath.Append(KFilename); 00036 TFileName srcFileName(filepath); 00037 00038 document = iDomParser.ParseFileL(srcFileName); 00039 CleanupClosePushL(document); 00040 TXmlEngElement element = document.DocumentElement().FirstChild().AsElement(); 00041 00042 _LIT8(str,"element"); 00043 _LIT8(str2,"wrong"); 00044 _LIT8(pref,"testns"); 00045 _LIT8(uri, "http://test.com"); 00046 00047 // Adds child element with no namespace 00048 element.AddNewElementL(str); 00049 00050 // Performs lookup for the namespace declaration for specified namespace URI and 00051 // adds new child element with found namespace. 00052 element.AddNewElementWithNsL(str,uri); 00053 00054 element = element.NextSibling().AsElement(); 00055 00056 // Performs lookup for the namespace declaration for specified prefix and 00057 // adds new child element with found namespace. 00058 element.AddNewElementUsePrefixL(str2,pref); 00059 00060 _LIT8(str4,"element3"); 00061 element = element.NextSibling().NextSibling().AsElement(); 00062 00063 // Copies a list of elements.Elements are appended to the element's children list. 00064 element.CopyChildrenL(document.DocumentElement().FirstChild().AsElement()); 00065 00066 _LIT8(uri3, "http://test.test.pl"); 00067 element = element.NextSibling().AsElement(); 00068 00069 // Creates new child element; if there is no a prefix binding for new element's namespace, 00070 // a namespace decaration is created with generated prefix at specified element. 00071 element.AddNewElementAutoPrefixL(str4,uri3,document.DocumentElement()); 00072 00073 //Save the document to verify manually 00074 TBuf<KFilePathSize> filepath2(iDrive); 00075 _LIT(KFilename2, "\\xedomexample\\output\\XML_addelements_output.xml"); 00076 filepath2.Append(KFilename2); 00077 document.SaveL(filepath2); 00078 00079 // To demonstrate removing of an element 00080 TXmlEngNode node1 = document.DocumentElement(); 00081 TXmlEngNode node2 = node1.FirstChild().Unlink(); 00082 node2 = node1.LastChild().SubstituteForL(node2); 00083 node2.Remove(); 00084 node1.FirstChild().Remove(); 00085 00086 CleanupStack::PopAndDestroy(); //document 00087 } 00088 00089 void CXEDomEx::SetElementTextL() 00090 { 00091 RXmlEngDocument document; 00092 TBuf<KFilePathSize> filepath(iDrive); 00093 _LIT(KFilename, "\\xedomexample\\input\\XML_setelement_text.xml"); 00094 filepath.Append(KFilename); 00095 TFileName srcFileName(filepath); 00096 00097 document = iDomParser.ParseFileL(srcFileName); 00098 CleanupClosePushL(document); 00099 TXmlEngElement element = document.DocumentElement().FirstChild().AsElement(); 00100 00101 _LIT8(str4,"test"); 00102 if(element.Text().Compare(str4) != 0) 00103 { 00104 User::Leave(KErrGeneral); // or some appropriate error 00105 } 00106 00107 _LIT8(str,"new text"); 00108 _LIT8(str2," new added text"); 00109 _LIT8(str3,"test&test1;test"); 00110 00111 // Sets text contents for the element. Any child nodes are removed 00112 element.SetTextL(str); 00113 00114 // Adds text as a child of the element 00115 element.AddTextL(str2); 00116 00117 element = element.NextSibling().AsElement(); 00118 00119 // Sets text content of the element from escaped string. 00120 element.SetEscapedTextL(str3); 00121 00122 //Save the document to verify manually 00123 TBuf<KFilePathSize> filepath2(iDrive); 00124 _LIT(KFilename2, "\\xedomexample\\output\\XML_addelements_output.xml"); 00125 filepath2.Append(KFilename2); 00126 document.SaveL(filepath2); 00127 00128 CleanupStack::PopAndDestroy(); //document 00129 } 00130
|
© Nokia 2009 |