|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * XEDOMATTRIBUTES.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 demonstrates how xml engine DOM deals 00014 * with attributes. It shows checking an element whether 00015 * it has an attribute, getting/setting attribute name/values, 00016 * adding/removing attributes, creating/deleting an attribute 00017 * node, copying attributes, attributes with namespaces etc. 00018 ***********************************************************/ 00019 00020 EXPORT_C void CXEDomEx::AttributesExampleL() 00021 { 00022 // some basic attribute operations - subexample 00023 AttributeOperationsL(); 00024 00025 // different ways of adding attributes - subexample 00026 AddAttributesL(); 00027 } 00028 00029 void CXEDomEx::AttributeOperationsL() 00030 { 00031 RXmlEngDocument document; 00032 TBuf<KFilePathSize> filepath(iDrive); 00033 _LIT(KFilename, "\\xedomexample\\input\\XML_check_attrs.xml"); 00034 filepath.Append(KFilename); 00035 TFileName srcFileName(filepath); 00036 00037 document = iDomParser.ParseFileL(srcFileName); 00038 CleanupClosePushL(document); 00039 TXmlEngElement element = document.DocumentElement().FirstChild().AsElement(); 00040 00041 _LIT8(str,"attr2"); 00042 _LIT8(str2,"wrong"); 00043 _LIT8(str3,"tewt"); 00044 _LIT8(str4,"attr3"); 00045 00046 //Check whether this element has the given attribute 00047 if(!(element.HasAttributeL(str,KNullDesC8))) 00048 { 00049 User::Leave(KErrGeneral); // or some appropriate error 00050 } 00051 00052 //Gets the attribute node for the element 00053 TXmlEngAttr attr = element.AttributeNodeL(str,KNullDesC8); 00054 if(attr.Value().Compare(str3) != 0) 00055 { 00056 User::Leave(KErrGeneral); // or some appropriate error 00057 } 00058 00059 //Modify Attributes 00060 element = document.DocumentElement().FirstChild().AsElement(); 00061 00062 // Removes the attribute from the element 00063 element.RemoveAttributeL(str4,KNullDesC8); 00064 00065 // Sets value of attribute; attribute is created if there is no such attribute yet 00066 element.SetAttributeL(str,str2); 00067 00068 // Creates new attribute, 00069 TXmlEngAttr attr1 = document.CreateAttributeL(str4); 00070 00071 // Sets new value of the attribute. Provided new value will be escaped as needed. 00072 attr1.SetValueL(str4); 00073 00074 // Links attribute into tree (by setting the attribute node for the element) 00075 element.SetAttributeNodeL(attr1); 00076 00077 //Save the document to verify manually 00078 TBuf<KFilePathSize> filepath2(iDrive); 00079 _LIT(KFilename2, "\\xedomexample\\output\\XML_modifattrs_output.xml"); 00080 filepath2.Append(KFilename2); 00081 document.SaveL(filepath2); 00082 00083 CleanupStack::PopAndDestroy(); //document 00084 } 00085 00086 void CXEDomEx::AddAttributesL() 00087 { 00088 RXmlEngDocument document; 00089 TBuf<KFilePathSize> filepath(iDrive); 00090 _LIT(KFilename, "\\xedomexample\\input\\XML_nsprocessing.xml"); 00091 filepath.Append(KFilename); 00092 TFileName srcFileName(filepath); 00093 00094 document = iDomParser.ParseFileL(srcFileName); 00095 CleanupClosePushL(document); 00096 TXmlEngElement element = document.DocumentElement().FirstChild().AsElement(); 00097 00098 _LIT8(str,"attr2"); 00099 _LIT8(str2,"wrong"); 00100 _LIT8(pref,"testns"); 00101 _LIT8(uri, "http://test.com"); 00102 00103 // Creates new attribute node 00104 element.AddNewAttributeL(str,str2); 00105 00106 // Creates new attributes using namespace in the scope, which has specified URI 00107 element.AddNewAttributeWithNsL(str,str2,uri); 00108 00109 element = element.NextSibling().AsElement(); 00110 00111 // Creates new attribute node using namespace of its parent element (this element) 00112 element.AddNewAttributeSameNsL(str,str2); 00113 00114 //Creates new attribute using namespace, which is bound to the specified prefix 00115 element.AddNewAttributeUsePrefixL(str2,str,pref); 00116 00117 _LIT8(str4,"attr3"); 00118 _LIT8(pref2,"testns2"); 00119 _LIT8(uri2, "http://test.com.pl"); 00120 element.AddNewAttributeL(str4,str2,uri2,pref2); 00121 00122 element = element.NextSibling().NextSibling().AsElement(); 00123 00124 // Resets element's attributes 00125 element.RemoveAttributes(); 00126 00127 // Copies attributes from another element 00128 element.CopyAttributesL(document.DocumentElement().FirstChild().AsElement()); 00129 00130 element = element.NextSibling().AsElement(); 00131 TXmlEngNamespace elementNs = element.FindOrCreateNsDeclL(uri2,pref2); 00132 element.AddNewAttributeL(str4,str,elementNs); 00133 00134 //Save the document to verify manually 00135 TBuf<KFilePathSize> filepath2(iDrive); 00136 _LIT(KFilename2, "\\xedomexample\\output\\XML_addattrs_output.xml"); 00137 filepath2.Append(KFilename2); 00138 document.SaveL(filepath2); 00139 00140 CleanupStack::PopAndDestroy(); //document 00141 }
|
© Nokia 2009 |