|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * XEDOMDOCPROCESSING.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 * Demonstrates the creation / buidling of a document from scratch. 00014 * Also shows how to deal with the different nodes 00015 * in the document and how operations of a node list 00016 ******************************************************************/ 00017 EXPORT_C void CXEDomEx::DocProcessingExampleL() 00018 { 00019 //creating a new document - sub example 00020 CreateDocumentL(); 00021 00022 // Perform some sibling operations 00023 SiblingOperationsL(); 00024 00025 // How to use node list 00026 NodeListOperationsL(); 00027 } 00028 00029 void CXEDomEx::CreateDocumentL() 00030 { 00031 _LIT8(name,"doc"); 00032 _LIT8(uri,"http://test.com"); 00033 _LIT8(arg1,"1.0"); 00034 _LIT8(arg2,"http://test2.org"); 00035 _LIT8(elename,"test"); 00036 00037 RXmlEngDocument document; 00038 document.OpenL(iDomImpl); 00039 CleanupClosePushL(document); 00040 00041 //Create a new document 00042 TXmlEngElement docEle = document.CreateDocumentElementL(name); // Creates and returns a document element 00043 document.DocumentElement().SetDefaultNamespaceL(uri); // Sets the default namespace of the document 00044 document.SetStandalone(TRUE); // Sets the standalone status of the document 00045 document.SetXmlVersionL(arg1); // Sets the XML version 00046 document.SetDocumentUriL(arg2); // Sets the document URI 00047 00048 TXmlEngElement ele= document.CreateElementL(elename); // Creates an element node 00049 docEle.AppendChildL(ele); // And make the element node a child to the document 00050 00051 //Save the document to verify manually 00052 TBuf<KFilePathSize> filepath(iDrive); 00053 _LIT(KFilename, "\\xedomexample\\output\\XML_createdoc_output.xml"); 00054 filepath.Append(KFilename); 00055 document.SaveL(filepath); 00056 00057 CleanupStack::PopAndDestroy(); //document 00058 } 00059 00060 00061 void CXEDomEx::SiblingOperationsL() 00062 { 00063 RXmlEngDocument document; 00064 TBuf<KFilePathSize> filepath(iDrive); 00065 _LIT(KFilename, "\\xedomexample\\input\\XML_sibling_fns.xml"); 00066 filepath.Append(KFilename); 00067 TFileName srcFileName(filepath); 00068 00069 document = iDomParser.ParseFileL(srcFileName); 00070 CleanupClosePushL(document); 00071 00072 // Retrieves the doc. element 00073 TXmlEngElement element = document.DocumentElement(); 00074 TXmlEngNode node; 00075 00076 // Sets the first child as last 00077 element.FirstChild().SetAsLastSibling(); 00078 00079 // Sets the previous child of last child as first one 00080 element.LastChild().PreviousSibling().SetAsFirstSibling(); 00081 00082 //Moves the last but one child before the child next to first child 00083 node = element.FirstChild().NextSibling(); 00084 element.LastChild().PreviousSibling().MoveBeforeSibling(node); 00085 00086 //Moves the next to next sibling of first child after last but one sibling 00087 node = element.LastChild().PreviousSibling(); 00088 element.FirstChild().NextSibling().NextSibling().MoveAfterSibling(node); 00089 00090 //Save the document to verify manually 00091 TBuf<KFilePathSize> filepath2(iDrive); 00092 _LIT(KFilename2, "\\xedomexample\\output\\XML_check_sibling_fns.xml"); 00093 filepath2.Append(KFilename2); 00094 document.SaveL(filepath2); 00095 00096 CleanupStack::PopAndDestroy(); //document 00097 } 00098 00099 00100 void CXEDomEx::NodeListOperationsL() 00101 { 00102 RXmlEngDocument document; 00103 TBuf<KFilePathSize> filepath(iDrive); 00104 _LIT(KFilename, "\\xedomexample\\input\\XML_nodetree_modif.xml"); 00105 filepath.Append(KFilename); 00106 TFileName srcFileName(filepath); 00107 00108 document = iDomParser.ParseFileL(srcFileName); 00109 CleanupClosePushL(document); 00110 00111 TXmlEngNode node; 00112 RXmlEngNodeList<TXmlEngElement> nodeList; 00113 node = document.DocumentElement(); 00114 00115 // Gets all the chidren elements of current node ( root element) 00116 node.AsElement().GetChildElements(nodeList); 00117 00118 // Gets the no.of nodes in the list 00119 if(nodeList.Count()!=4) 00120 User::Leave(KErrGeneral); 00121 CleanupClosePushL(nodeList); 00122 00123 TXmlEngElement element; 00124 00125 // Returns next node 00126 element = nodeList.Next(); 00127 00128 // Check the name of first element 00129 _LIT8(name,"elem1"); 00130 if(element.Name().Compare(name)) 00131 User::Leave(KErrGeneral); 00132 00133 nodeList.Next(); 00134 nodeList.Next(); 00135 00136 // Checks whether there is a node 00137 if (nodeList.HasNext()) 00138 { 00139 element = nodeList.Next(); 00140 _LIT8(name1,"elem4"); 00141 if(element.Name().Compare(name1)) 00142 User::Leave(KErrGeneral); 00143 } 00144 00145 CleanupStack::PopAndDestroy(); //nodeList 00146 CleanupStack::PopAndDestroy(); //document 00147 }
|
© Nokia 2009 |