The Traversal method of cmarkup attributes and child elements

Source: Internet
Author: User

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.