Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to code simple XML parser?

xml

  • Please log in to reply
8 replies to this topic

#1 Hamed

Hamed

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts

Posted 15 March 2012 - 08:48 PM

Hello,
Can anyone help me to make simple XML parser?
#include <cstring>

using namespace std;

int main() {
fstream curStream;
char file[] = "file.xml";
char cur;
char id[] = "id>";
int i = 0;
curStream.open(file, ios::in);
while (!curStream.eof()) {
curStream >> cur;
if(cur == '<') {
curStream >> cur;
//Commet & Ending & Standard
if(cur == '!' || cur == '?' || cur == '/') {
char rCur;
while (rCur != '>') {
curStream >> rCur;
cout << rCur;
}
} else {
//Find tag that we want
char tCur;
char tag[100];
strcat(tag, &cur);
while(tCur != '>') {
curStream >> tCur;
strcat(tag, &tCur);
}
if(!strcmp(tag, id)) {
cout << tag;
}

}
}
}
cout << i;
return 0;
}

I want to find tag and write data of that tag!
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 16 March 2012 - 06:06 PM

I made mine with a recursive function.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Hamed

Hamed

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts

Posted 18 March 2012 - 10:38 PM

Can I have it?
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 19 March 2012 - 03:10 PM

Unfortunately, it was very limited, and done in C#. If you're still interested, I'll see if I can dig it up.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 Hamed

Hamed

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts

Posted 19 March 2012 - 07:33 PM

Give it to me the algorithm is important for me.
  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 March 2012 - 03:47 PM

Here's the relevant code
        private void openToolStripButton_Click(object sender, EventArgs e) 
        { 
            openFileDialog1.FileName = filename; 
            if (openFileDialog1.ShowDialog() == DialogResult.OK) 
            { 
                filename = openFileDialog1.FileName; 
                StreamReader infile = new StreamReader(filename); 
                if (infile.ReadLine().Equals("<root>")) 
                { 
                    treeView1.Nodes.Clear(); 
                    if (!loadNodes(treeView1.Nodes, infile, "  ", "</root>")) 
                    { 
                        MessageBox.Show("Error, Invalid file format!"); 
                        treeView1.Nodes.Clear(); 
                    } 
                    else 
                    { 
                        treeView1.SelectedNode = treeView1.Nodes[0]; 
                    } 
                } 
                else 
                { 
                    MessageBox.Show("Error, Invalid file format!"); 
                } 
                infile.Close(); 
            } 
        } 
 
        private bool loadNodes(TreeNodeCollection theNodes, StreamReader infile, string pad, string endnode) 
        { 
            string input = infile.ReadLine(); 
            bool bValid = true; 
            while ((!input.Equals(endnode))&&bValid) 
            { 
                if (input.Equals(pad + "<node>")) 
                { 
                    input = parseText(infile.ReadLine()); 
                    TreeNode newNode = new TreeNode(input); 
                    theNodes.Add(newNode); 
                    bValid = loadNodes(newNode.Nodes, infile, pad + "  ", pad + "</node>"); 
                    if (newNode.Nodes.Count > 0) 
                    { 
                        newNode.ImageIndex = 0; 
                        newNode.SelectedImageIndex = 0; 
                    } 
                    else 
                    { 
                        newNode.ImageIndex = 1; 
                        newNode.SelectedImageIndex = 1; 
                    } 
 
                } 
                else 
                { 
                    bValid = false; 
                } 
                input = infile.ReadLine(); 
            } 
            return bValid; 
        } 
 
        private string parseText(string input) 
        { 
            StringBuilder output = new StringBuilder(input.Trim()); 
            output.Replace("<text>", ""); 
            output.Replace("</text>", ""); 
            output.Replace("<", "<"); 
            output.Replace(">", ">"); 
            output.Replace("'","'"); 
            output.Replace(""","\""); 
            output.Replace("&","&"); 
 
            return output.ToString(); 
        } 
 
        private void saveToolStripButton_Click(object sender, EventArgs e) 
        { 
            bool bValid = false; 
            if (filename.Equals("")) 
            { 
                if (saveFileDialog1.ShowDialog() == DialogResult.OK){ 
                    filename = saveFileDialog1.FileName; 
                    bValid = !filename.Equals(""); 
                } 
            } 
            else 
            { 
                bValid = true; 
            } 
            if (bValid) 
            { 
                StreamWriter outfile = new StreamWriter(filename); 
                string pad = "  "; 
                outfile.WriteLine("<root>"); 
                dumpNodes(treeView1.Nodes, outfile, pad); 
                outfile.WriteLine("</root>"); 
                outfile.Close(); 
            } 
 
        } 
 
        private void dumpNodes(TreeNodeCollection theNodes, StreamWriter outfile, string pad) 
        { 
            foreach (TreeNode item in theNodes) 
            { 
                outfile.WriteLine(pad + "<node>"); 
                outfile.WriteLine(pad + "  <text>" + escapeXML(item.Text) + "</text>"); 
                dumpNodes(item.Nodes, outfile, pad + "  "); 
                outfile.WriteLine(pad + "</node>"); 
 
            } 
        } 
 
        private string escapeXML(string instr) 
        { 
            StringBuilder output = new StringBuilder(""); 
            for (int i = 0; i < instr.Length; i++) 
            { 
                switch (instr[i]) 
                { 
                    case '<' :  
                        output.Append("<"); 
                        break; 
                    case '>' : 
                        output.Append(">"); 
                        break; 
                    case '&' : 
                        output.Append("&"); 
                        break; 
                    case '\'' : 
                        output.Append("'"); 
                        break; 
                    case '"' : 
                        output.Append("""); 
                        break; 
                    default : 
                        output.Append(instr[i]); 
                        break; 
                } 
 
            } 
            return output.ToString(); 
        } 



  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 ysangkok

ysangkok

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 24 March 2012 - 12:26 PM

IMHO, it makes little sense to write your own XML parsers when there are so many out there. For example, look here: Using MSXML to read XML documents - CodeProject® for an MSXML tutorial. You can filter by IElem::name() to get elements with the desired tag name.
  • 0

#8 redvms

redvms

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 29 March 2012 - 11:28 PM

[ATTACH=CONFIG]4677[/ATTACH]

---------- Post added at 11:28 PM ---------- Previous post was at 11:24 PM ----------

http://ironhammers.o...011/06/good.gif
  • 0

#9 redvms

redvms

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 29 March 2012 - 11:56 PM

Posted Image


  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download