Use msxml4 to create an element
Zhhx1981 (Feifei)
22:50:58 ask a question in VC/mfc/html/XML
Code For more information, see.
Ixmldomelement * cactionxml: buildnode (cstring & snodename,
Cstring & sattrname, cstring & sattrvalue, cstring & stext,
Ixmldomdocument * pdom)
{
Ixmldomelement * Pe = NULL;
Ixmldomattribute * pA = NULL;
Ixmldomattribute * pa1 = NULL;
BSTR = NULL;
Variant var;
Hresult hr;
Variantinit (& var );
// Create an element
// BSTR = sysallocstring (L "kjkj"); // if this sentence is used Program Succeeded;
If (! Snodename. isempty ()){
BSTR = snodename. allocsysstring (); // use this sentence to make the next sentence fail
HR = pdom-> createelement (BSTR, & PE); // fail when the sentence is executed. Why?
Sysfreestring (BSTR );
BSTR = NULL;
}
// Create elment text
If (! Stext. isempty ()){
BSTR = stext. allocsysstring ();
HR = pe-> put_text (BSTR );
Sysfreestring (BSTR );
BSTR = NULL;
}
// Create the elemnt attribute
If (! Sattrname. isempty ()){
BSTR = sattrname. allocsysstring ();
Var = variantstring (sattrvalue. allocsysstring ());
HR = pdom-> createattribute (BSTR, & PA );
HR = pa-> put_value (VAR );
HR = pe-> setattributenode (Pa, & pa1 );
Sysfreestring (BSTR );
BSTR = NULL;
Variantclear (& var );
}
// Resource release
// If (PA) Pa-> release ();
// If (pa1) pa1-> release ();
// If (PE) Pe-> release ();
Return PE;
}
Number of questions: 50. replies: 2
Top
1st Floor
Liuxianzhi (Biyuan)
Back to 12:44:29 score
20
In this way:
Msxml2: ixmldomelement * ptempelement = NULL;
BSTR tempelementname = _ bstr_t (strtopelementname );
Hrerr = pxmldomdoc-> createelement (tempelementname, & ptempelement );
Sysfreestring (tempelementname );
If (hrerr = s_ OK)
// The top-level element node is successfully created.
{......}
Top
2nd floor
Zhp80 (zhp80)
Back to 14:38:43 score
30
See
Bool csysconfig: savedefault ()
{
Cstring xmlstring = "";
XML: ixmldomelementptr pxmlnoda = NULL;
XML: ixmldomelementptr pxmlnodb = NULL;
XML: ixmldomattributeptr pxmlattr = NULL;
XML: ixmldomdocument2ptr pxmlfile = NULL;
Pxmlfile. createinstance (_ uuidof (XML: domdocument40 ));
Xmlstring = "<? XML version = "1.0 \" encoding = \ "gb2312 \"
?> <Trafficcaptrue version = \ "1.0 \"
> </Trafficcapture> ";
Pxmlfile-> loadxml (xmlstring. allocsysstring ());
If (! Pxmlfile-> parsed ){
Pxmlfile = NULL;
Return false;
}
XML: ixmldomelementptr pxmlroot = pxmlfile-> documentelement;
Pxmlnoda = pxmlfile-> createelement ("picture ");
Pxmlattr = pxmlfile-> createattribute ("width ");
Pxmlattr-> value = ccomvariant (mimagewidth );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("height ");
Pxmlattr-> value = ccomvariant (mimageheight );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("quality ");
Pxmlattr-> value = ccomvariant (mjpgquality );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("path ");
Pxmlattr-> value = ccomvariant (msavepath );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlroot-> appendchild (pxmlnoda );
Pxmlnoda = pxmlfile-> createelement ("queue ");
Pxmlattr = pxmlfile-> createattribute ("vehicle ");
Pxmlattr-> value = ccomvariant (mmaxvehiclequeue );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("closeshot ");
Pxmlattr-> value = ccomvariant (mcloseshotimagecount );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("Panorama ");
Pxmlattr-> value = ccomvariant (mpanoramaimagecount );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlroot-> appendchild (pxmlnoda );
Pxmlnoda = pxmlfile-> createelement ("DBMS ");
Pxmlattr = pxmlfile-> createattribute ("connstring ");
Pxmlattr-> value = ccomvariant (mconnstr );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("addcaptureinfo ");
Pxmlattr-> value = ccomvariant (mstoreprocedure );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("backlist ");
Pxmlattr-> value = ccomvariant (mblacklistview );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlroot-> appendchild (pxmlnoda );
Pxmlnoda = pxmlfile-> createelement ("rthost ");
Pxmlattr = pxmlfile-> createattribute ("hostname ");
Pxmlattr-> value = ccomvariant (MIP );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("commuport ");
Pxmlattr-> value = ccomvariant (MTCP );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlroot-> appendchild (pxmlnoda );
Pxmlnoda = pxmlfile-> createelement ("panoramasection ");
Pxmlattr = pxmlfile-> createattribute ("length ");
Pxmlattr-> value = ccomvariant (3 );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("video ");
Pxmlattr-> value = ccomvariant (0 );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlroot-> appendchild (pxmlnoda );
Pxmlnoda = pxmlfile-> createelement ("mechanic ");
Pxmlattr = pxmlfile-> createattribute ("OCR ");
Pxmlattr-> value = ccomvariant (mocr );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("saveall ");
Pxmlattr-> value = ccomvariant (msaveall );
Pxmlnoda-> attributes-> setnameditem (pxmlattr );
Pxmlroot-> appendchild (pxmlnoda );
Pxmlnoda = pxmlfile-> createelement ("peripheral ");
Pxmlnodb = pxmlfile-> createelement ("solonc ");
Pxmlattr = pxmlfile-> createattribute ("Comport ");
Pxmlattr-> value = ccomvariant (msoloncport );
Pxmlnodb-> attributes-> setnameditem (pxmlattr );
Pxmlattr = pxmlfile-> createattribute ("portsetting ");
Pxmlattr-> value = ccomvariant ("38400, N, 8, 1 ");
Pxmlnodb-> attributes-> setnameditem (pxmlattr );
Pxmlnoda-> appendchild (pxmlnodb );
Pxmlroot-> appendchild (pxmlnoda );
Xmlstring = "trafficcapture. cfg ";
Pxmlfile-> Save (xmlstring. allocsysstring ());
Pxmlfile = NULL;
Return true;
}