Use C to read XML files

Source: Internet
Author: User

 

The Tree of XML files (with only one top-level node) can be used as a storage structure of XML.
Here I will use C ++ to parse simple XML files.
1. Select the storage structure:
The tree data structure has multiple storage methods. I will use the "Child brother notation", which is defined as follows:
Typedef struct csnode
{
Int subnodes;
String data;
String name;
Struct csnode * firstchild, * nextsibling, * parent;
Csnode * operator = (csnode cNode)
{
This-> DATA = cNode. Data;
This-> firstchild = cNode. firstchild;
This-> name = cNode. Name;
This-> nextsibling = cNode. nextsibling;
This-> subnodes = cNode. subnodes;
Return this;
}
} Csnode, * cstree;

2. Define an ADT to provide a public interface for operations. Get the name XML

Class XML
{
Public:
XML ();
XML (char * filename );
~ XML ();
Csnode & createtree (); // create a storage structure
Bool finddata (const char * nodepos); // find the node Value
Bool finddata (const char * partent, const char * child, string * data); // find the node Value
Bool readfile _ (); // read the XML source file
Void allocate (); // release node resources
PRIVATE:
String _ filepath;
Char * _ filename;
Csnode * head;

};

3. Specific implementation

# Include "stdafx. H"
# Include "xmlcreate. H"
# Include "wstack. H"
# Include <algorithm>
Using namespace STD;
XML: XML ()
{
}
XML: XML (char * filename): _ filename (filename)
{
Head = new csnode;
}
XML ::~ XML ()
{
Delete head;
}
Csnode & xml: createtree ()
{
Csnode * P = new csnode;
Csnode * q = new csnode;
Csnode * s = new csnode;
Wstack <string> namestack;
Wstack <csnode> nodestack;
String tmpstr;
String name, tempname, rootname, headname;
Int noods = 0;
Bool subroot = true, next = false, poproot = false;
Unsigned short int enoods = 0;
Char pS;
For (size_t I = 0; I <_ filepath. Size (); ++ I ){
PS = _ filetings [I];
If (_ filepath [I] = ''| _ filepath [I] ='' | _ filepath [I] = '/T' | _ filepath [i] = 0x09 | _ file1_[ I] = 0x0d) continue;
If (_ filepath [I] = '<' & _ filepath [I + 1]! = '/'){
S = new csnode;
S-> subnodes = 0;
Enoods = 0;
}
Else if (_ filetings [I] = '> '){
Enoods = 0;
S-> name = tmpstr;
Namestack. Push (tmpstr );
Tmpstr = "";
Noods ++;
Size_t Pn = I + 1;
Char bug;
While (PN <_ filetings. Size ()){
Bug = _ filepath [pn];
If (_ filepath [pn] = ''| _ filepath [pn] ='' | _ filepath [pn] = '/T' | _ filepath [pn] = 0x09 | _ filepath [pn] = 0x0d)
{
Pn ++;
Continue;
}
Else break;
}

If (_ filepath [pn] = '<'){
Subroot = true; // parses up
}

If (noods = 1) {// Save the root node
Head = s;
Head-> parent = NULL;
Head-> nextsibling = NULL;
P = head;
Headname = head-> name;
}
Else if (subroot) {// The leaf node has not been resolved
If (poproot ){
P-> nextsibling = s;
P = p-> nextsibling;
Poproot = false;
}
Else {
S-> parent = P;
P-> firstchild = s;
P = p-> firstchild;
}
}
Else if (! Subroot) {// parse to the leaf node (2nd)
S-> parent = P;
P-> nextsibling = s;
P-> firstchild = NULL;
P = p-> nextsibling;
}
}
Else if (_ filepath [I] = '<' & _ filepath [I + 1] = '/'){
Enoods ++;
// P-> firstchild = NULL;
If (subroot & enoods <2 ){
Q = p-> parent;
Rootname = Q-> name;
Subroot = false;
}
If (! Subroot) q-> subnodes ++;
String tname = namestack. Pop ();
I = I + tname. Size () + 2;
If (tmpstr. Size ()> 0 ){
S-> DATA = tmpstr;
Tmpstr = "";
}
Else {
Subroot = true;
Next = false;
}
If (tname = rootname ){
P-> nextsibling = NULL;
P-> firstchild = NULL;
P = Q;
Poproot = true;
}
Else if (tname = headname ){
Q-> nextsibling = NULL;
}
}
Else
{
Enoods = 0;
If (_ filetings [I]! = '/') Tmpstr + = _ filetings [I];
}
}
Delete S;
Return * head;
}
Bool XML: readfile _()
{
Ifstream infile (_ filename, IOS: Binary );
If (! Infile ){
// Afxmessagebox ("openfile failed! ");
Return false;
}
Bool lable = false;
String _ TFC (""), _ lable ("");
Char C;
While (infile. Get (c) _ TFC + = C;
For (size_t I = 0; I <_ TFC. Size (); ++ I ){
If (_ TFC [I] = ''| _ TFC [I] ='' | _ TFC [I] = '/T' | _ TFC [i] = 0x09 | _ TFC [I] = 0x0d) continue;
If (_ TFC [I] = '<' & _ TFC [I + 1] = '? '){
Lable = true;
}
Else if (_ TFC [I] = '? '& _ TFC [I + 1] ='> '){
Lable = false;
_ Lable + = _ TFC [I];
_ Lable + = _ TFC [I + 1];
I + = 2;
Continue;
}
Else if (_ TFC [I] = '<' & _ TFC [I + 1] = '! '& _ TFC [I + 2] = '-'){
Lable = true;
}
Else if (_ TFC [I] = '-' & _ TFC [I + 1] = '-' & _ TFC [I + 2] = '> ') {
Lable = false;
_ Lable + = _ TFC [I];
_ Lable + = _ TFC [I + 1];
_ Lable + = _ TFC [I + 2];
I + = 3;
}
If (lable) _ lable + = _ TFC [I];
Else _ filepath + = _ TFC [I];
}
Return true;
}
Bool XML: finddata (const char * nodename)
{
Csnode * P = head;
String _ nodename = nodename;
Return true;
}
Bool XML: finddata (const char * parent, const char * child, string * Data)
{
Csnode * P = head-> firstchild;
String _ parent (""), _ child ("");
Bool isfound = false;
While (P! = NULL ){
If (p-> name = parent ){
P = p-> firstchild;
While (P! = NULL ){
If (p-> name = Child ){
* Data = p-> data;
Return true;
}
Else P = p-> nextsibling;
}
}
Else P = p-> nextsibling;
}
Return false;
}
Void XML: allocate ()
{
Csnode * P = head-> firstchild;
While (P! = NULL ){
Csnode * q = p-> firstchild;
While (Q! = NULL ){
Csnode * s = Q;
Q = Q-> nextsibling;
If (S-> name = "recvport "){
Cout <"addd" <Endl;
}
Delete S;
}
Csnode * m = P;
P = p-> nextsibling;
Delete m;
}
Delete head;
}

Currently, this program can only Parse XML files with three layers of Structure

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.