467,927 Members | 1,955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,927 developers. It's quick & easy.

Reading XML file using TinyXML

hello,

can you please help me in reading this sample xml using tinyxml library.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <Configuration>
  4.     <Parameter name="loglevel" value="3"/>
  5.     <Parameter name="newparam" value="some value"/>
  6.     <Process Name="osis">
  7.         <App ID="1" Name="TATA ADC" LogPath="C:\Projects\TATA\TATA\TATA ADC\Test" SeparatorLine="======">
  8.             <LineFormat Separator="|">
  9.                 <LinePart>Timestamp</LinePart>
  10.                 <LinePart>InformationType</LinePart>
  11.                 <LinePart>Message</LinePart>
  12.             </LineFormat>
  13.             <Block StartKeyword="processing fail"/>
  14.             <Block StartKeyword="processing chain"/>
  15.             <MailRecipient>ABCD@ABC.com</MailRecipient>
  16.         </App>
  17.         <App ID="1" Name="TATA Loader" LogPath="C:\Projects\TATA\TATA\TATA Loader\Test" SeparatorLine="---------">
  18.             <LineFormat Separator="|">
  19.                 <LinePart>Timestamp</LinePart>
  20.                 <LinePart>InformationType</LinePart>
  21.                 <LinePart>Message</LinePart>
  22.                 <LinePart>Detail</LinePart>
  23.                 <LinePart>Location</LinePart>
  24.             </LineFormat>
  25.             <Block StartKeyword="processing"/>
  26.             <MailRecipient>ABCD@ABC.com</MailRecipient>
  27.             <Alert Level="error"/>
  28.         </App>
  29.     </Process>
  30.     <Process Name="Ation">
  31.         <App ID="1" Name="ADA" LogPath="C:\Progloc\ADA" SeparatorLine="">
  32.             <LineFormat Separator=" ">
  33.                 <LinePart>Timestamp</LinePart>
  34.                 <LinePart>Message</LinePart>
  35.             </LineFormat>
  36.             <Block StartKeyword="performing"/>
  37.             <MailRecipient>ABCD@ABC.com</MailRecipient>
  38.         </App>
  39.     </Process>
  40.     <MailServer>smtp.intra.ABC.com</MailServer>
  41.     <MailSender>ABCDE@ABC.com</MailSender>
  42. </Configuration>
It's a bit urgent, please help me now.
Dec 6 '07 #1
  • viewed: 25379
Share:
9 Replies
I've framed in a bit of code to help you process this file. You have a lot in there, so what I gave you should be enough to get you started. I haven't included much cleanup or error handling, just implemented some basic parsing for you.

The output of the following code will look like this:

test.xml loaded
Parameter: name= 'loglevel', value='3'
Parameter: name= 'newparam', value='some value'

Process: osis
..App: ID=1, name=TATA ADC
..App: ID=1, name=TATA Loader
Process: Ation
..App: ID=1, name=ADA


Expand|Select|Wrap|Line Numbers
  1.     // Load the xml file, I put your XML in a file named test.xml
  2.     TiXmlDocument XMLdoc("test.xml");
  3.     bool loadOkay = XMLdoc.LoadFile();
  4.     if (loadOkay)
  5.     {
  6.         cout << "test.xml loaded" << endl;
  7.         TiXmlElement *pRoot, *pParm, *pProcess, *pApp, *pLineFormat;
  8.         pRoot = XMLdoc.FirstChildElement( "Configuration" );
  9.         if ( pRoot )
  10.         {
  11.             // Parse parameters
  12.             pParm = pRoot->FirstChildElement("Parameter");
  13.             while ( pParm )
  14.             {
  15.                 cout << "Parameter: name= '" << pParm->Attribute("name") << "', value='" << pParm->Attribute("value") << "'" << endl;
  16.                 pParm = pParm->NextSiblingElement( "Parameter" );
  17.             }
  18.  
  19.             cout << endl;
  20.  
  21.             // Parse Process 
  22.             pProcess = pRoot->FirstChildElement("Process" );
  23.             while ( pProcess )
  24.             {
  25.                 cout <<"Process: " << pProcess->Attribute("Name" ) << endl;
  26.                 pApp = pProcess->FirstChildElement("App" );
  27.                 while ( pApp )
  28.                 {
  29.                     cout << "..App: ID=" << pApp->Attribute("ID") << ", name=" << pApp->Attribute("Name") << endl;
  30.  
  31.                     // ***************************************************
  32.                     // * TODO: Put code in to handle the App details
  33.                     // ***************************************************
  34.  
  35.                     pApp = pApp->NextSiblingElement("App");
  36.                 }
  37.  
  38.                 pProcess = pProcess->NextSiblingElement("Process");
  39.             }
  40.         }
  41.         else
  42.         {
  43.             cout << "Cannot find 'Configuration' node" << endl;
  44.         }
  45.  
  46.  
I hope this helps
Bill
<removed>
Dec 12 '07 #2
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?> 
  2. <Planning>
  3.     <TwoDobject>
  4.         <Coordinates>
  5.             <Octagonshape>
  6.                 <Firstmodel>
  7.                     <Vertex x="0.000002" y="0.000004" z="0.00"> </Vertex>
  8.                     <Vertex x="0.000002" y="0.000005" z="0.00"> </Vertex>
  9.                     <Vertex x="0.000003" y="0.000006" z="0.00"> </Vertex>
  10.                     <Vertex x="0.000004" y="0.000006" z="0.00"> </Vertex>
  11.                     <Vertex x="0.000005" y="0.000005" z="0.00"> </Vertex>
  12.                     <Vertex x="0.000005" y="0.000004" z="0.00"> </Vertex>
  13.                     <Vertex x="0.000004" y="0.000003" z="0.00"> </Vertex>
  14.                     <Vertex x="0.000003" y="0.000003" z="0.00"> </Vertex>                
  15.                 </Firstmodel>
  16.             </Octagonshape>
  17.             <Rectangleshape>
  18.                 <Firstmodel>
  19.                 </Firstmodel>
  20.             </Rectangleshape>
  21.         </Coordinates>
  22.         <Firstscene>
  23.             <Octagonshape>
  24.                 <Firstmodel>
  25.                 <Transformation x="0.0000035" y="0.0000045" theta=0.0> </Transformation>
  26.                 </Firstmodel>
  27.             </Octagonshape>
  28.             <Rectangleshape>
  29.                 <Firstmodel>
  30.                 </Firstmodel>
  31.             </Rectangleshape>
  32.         </Firstscene>        
  33.     </TwoDobject>
  34.     <ThreeDobject>
  35.     </ThreeDobject>
  36. </Planning>
Hi I have this xml file. I want to parse this . Can anyone help me.
Thanks
Sagar
Sep 30 '09 #3
Dormilich
Expert Mod 8TB
@sagar353
I can offer help with PHP.
Sep 30 '09 #4
Thanks Dormillich. Actually, I have no experience in PHP. Basically, I want to put all the information in the xml file into a data structure. But I dont know how to do that? Can you help me with some logic.
Sagar
Sep 30 '09 #5
Dormilich
Expert Mod 8TB
@sagar353
you didnít say anything about what language you wanted to use. PHP is simply one of the languages Iím good at.
@sagar353
what kind of structure, arrays, objects, Ö?
Sep 30 '09 #6
Hi Dormilich,
I want to define different classes and objects where I will store the information in a structured way. Thanks
Sagar
Sep 30 '09 #7
Dormilich
Expert Mod 8TB
you mean something like serialization?

one exchangeable serializer is WDDXÖ
Sep 30 '09 #8
I dont know what WDDX mean. Since I already organize my XML file, serialization will work probably.
Thanks
Sagar
Sep 30 '09 #9
Dormilich
Expert Mod 8TB
WDDX is a XML based serializer.
Oct 1 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Tony Stock | last post: by
3 posts views Thread by syntax | last post: by
Meetee
reply views Thread by Meetee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.