XML file of the property style:
<Root>
<Userinfo userid = "User code-insurance company business system" username = "User Name-insurance company business system"
Dptid = "department code-insurance company business system" dptname = "department name-insurance company business system"/>
</Root>
Traverse all attributes of the userinfo element and convert them into child element styles. The method is as follows:
Cmarkup sxml, dxml; // source file (Format 1, attribute style) and target file (Format 2, element style)
Dxml. setdoc (cstring ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> /R/N "));
Cstring filepath;
Getdlgitemtext (idc_efilepath, filepath );
Sxml. Load (filepath); // load the XML file in the specified format.
Sxml. findelem (Root );
Dxml. addelem (Root );
Dxml. cancelem ();
Sxml. cancelem (); // into root
For (INT I = 0; I <nodes-> getlength (); I ++) // The root sub-node s of the outermost layer
{
Sxml. findelem (nodes [I]);
Dxml. addelem (nodes [I]);
Dxml. cancelem ();
Cstring attribval;
Cstring attribname;
// Core code
For (INT attribindex = 0; attribindex ++)
{
Attribname = sxml. getattribname (attribindex );
If (attribname. getlength ()! = 0) // If the empty string is returned for the method, it indicates that the attribute ends and the loop ends.
{
Attribval = sxml. getattrib (attribname); // otherwise, read the attribute value and add the child element to dxml.
Dxml. addelem (attribname, attribval );
}
Else
Break;
}
Dxml. outofelem ();
}
Dxml. Save (cstring ("syntax 2.xml "));
XML file of child element style:
<Root>
<Userinfo>
<Userid> User code-Insurance Company Business System </userid>
<Username> User Name-Insurance Company Business System </username>
<Dptid> User Name-Insurance Company Business System </dptid>
<Dptname> Department name-Insurance Company Business System </dptname>
<Unuserid> User code-operation platform </unuserid>
<Unusername> User Name-operation platform </unusername>
<Undptid> department code-operation platform </undptid>
<Undptname> Department name-operation platform </undptname>
<Comdptid> the insurance company to which the current claim business belongs, such as ycbx </comdptid>
</Userinfo>
</Root>
Traverse all sub-elements of userinfo and convert them to attribute styles. The method is as follows:
Cmarkup sxml, dxml; // source file (Format 2, element style) and target file (Format 1, attribute style)
Dxml. setdoc (cstring ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> /R/N "));
Cstring filepath;
Getdlgitemtext (idc_efilepath, filepath );
Sxml. Load (filepath );
Sxml. findelem (Root );
Dxml. addelem (Root );
Dxml. cancelem ();
Sxml. cancelem (); // into root
For (INT I = 0; I <nodes-> getlength (); I ++) // The root sub-node s of the outermost layer
{
Sxml. findelem (nodes [I]);
Dxml. addelem (nodes [I]);
Sxml. cancelem (); // into this subnode
Cstring elemval;
Cstring elemname;
// Core code
While (sxml. findelem () // if no parameter is added to the method, the next sibling node is searched. If no subnode is followed, false is returned.
{
// Remove sub-element names and values
Elemname = sxml. gettagname (); // obtain the child element name
Elemval = sxml. getdata (); // Value
Dxml. setattrib (elemname, elemval); // Add a new attribute
}
Sxml. outofelem (); // out the subnode to root
}
Dxml. Save (cstring ("format 2_1.xml "));
In fact, smart people will find that the root-level subnode traversal of the outermost layers of the two methods can be completed using the second method to traverse sub-elements. That is, the nesting of the two methods is used. The outermost layer traverses sub-elements, and the first method can be used to traverse each of its attributes for each sub-element.
Next, modify the first property Traversal method as follows: (element traversal nested property traversal)
Cmarkup sxml, dxml; // source file (Format 1, attribute style) and target file (Format 2, element style)
Dxml. setdoc (cstring ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> /R/N "));
Cstring filepath;
Getdlgitemtext (idc_efilepath, filepath );
Sxml. Load (filepath );
Sxml. findelem (Root );
Dxml. addelem (Root );
Dxml. cancelem ();
Sxml. cancelem (); // into root
While (sxml. findelem ())
{
Dxml. addelem (sxml. gettagname ());
Dxml. cancelem ();
Cstring attribval;
Cstring attribname;
For (INT attribindex = 0; attribindex ++)
{
Attribname = sxml. getattribname (attribindex );
If (attribname. getlength ()! = 0)
{
Attribval = sxml. getattrib (attribname );
Dxml. addelem (attribname, attribval );
}
Else
Break;
}
Dxml. outofelem ();
}
Dxml. Save (cstring ("syntax 2.xml "));
Of course, if you are smart, you will naturally think of such nesting. Element traversal is the improved version of the second method above:
Cmarkup sxml, dxml; // source file (Format 2, element style) and target file (Format 1, attribute style)
Dxml. setdoc (cstring ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> /R/N "));
Cstring filepath;
Getdlgitemtext (idc_efilepath, filepath );
Sxml. Load (filepath );
Sxml. findelem (Root );
Dxml. addelem (Root );
Dxml. cancelem ();
Sxml. cancelem (); // into root
Cstring elemval;
Cstring elemname;
While (sxml. findelem () // each level-1 subnode of the root user
{
Elemname = sxml. gettagname ();
Dxml. addelem (elemname );
Sxml. cancelem ();
While (sxml. findelem () // subelements of each subnode
{
Elemname = sxml. gettagname ();
Elemval = sxml. getdata ();
Dxml. setattrib (elemname, elemval );
}
Sxml. outofelem (); // out to root
}
Dxml. Save (cstring ("format 2_1.xml "));
The above code is tested and passed in the vs2005c ++ environment.
I hope the above insights will help you better use cmarkup. O (distinct _ distinct) O ~