By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

extract the xml data values in the specified format using C++ code

P: 1
Hi All,

Please help me in writing the code for extracting the data values from the XML file using C++ code.
The xml file is as follows
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3.  
  4.   <!-- Configuration Type -->
  5.   <CfgType>
  6.     <Name>Ane Justin</Name>
  7.     <MachineModels>
  8.       <MachineModel>
  9.         <ModelNo>2345678</ModelNo>        
  10.         <MonitorTypes>          
  11.           <MonitorType>
  12.             <MonitorName>17" LCD</MonitorName>          
  13.             <ScreenResolution>
  14.               <Resolution>1024*768</Resolution>
  15.               <RefreshRate>60</RefreshRate>
  16.             </ScreenResolution>            
  17.             <ScreenResolution>
  18.               <Resolution>800*600</Resolution>
  19.               <RefreshRate>60</RefreshRate>
  20.             </ScreenResolution>
  21.           </MonitorType>          
  22.           <MonitorType>
  23.             <MonitorName>19" LCD</MonitorName>            
  24.             <ScreenResolution>
  25.               <Resolution>1024*768</Resolution>
  26.               <RefreshRate>60</RefreshRate>
  27.             </ScreenResolution>            
  28.             <ScreenResolution>
  29.               <Resolution>800*600</Resolution>
  30.               <RefreshRate>60</RefreshRate>
  31.             </ScreenResolution>          
  32.           </MonitorType>          
  33.         </MonitorTypes>        
  34.       </MachineModel>
  35.       <MachineModel>
  36.         <ModelNo>0789456</ModelNo>
  37.         <MonitorTypes>
  38.           <MonitorType>
  39.             <MonitorName>17" CRT</MonitorName>
  40.             <ScreenResolution>
  41.               <Resolution>1024*768</Resolution>
  42.               <RefreshRate>60</RefreshRate>
  43.             </ScreenResolution>
  44.             <ScreenResolution>
  45.               <Resolution>800*600</Resolution>
  46.               <RefreshRate>60</RefreshRate>
  47.             </ScreenResolution>
  48.           </MonitorType>
  49.           <MonitorType>
  50.             <MonitorName>19" CRT</MonitorName>
  51.             <ScreenResolution>
  52.               <Resolution>1024*768</Resolution>
  53.               <RefreshRate>60</RefreshRate>
  54.             </ScreenResolution>
  55.             <ScreenResolution>
  56.               <Resolution>800*600</Resolution>
  57.               <RefreshRate>60</RefreshRate>
  58.             </ScreenResolution>
  59.           </MonitorType>
  60.         </MonitorTypes>
  61.       </MachineModel>
  62.     </MachineModels>
  63.   </CfgType>
  64.  
  65. <CfgType>
  66.     <Name>Jessica Sis</Name>
  67.     <MachineModels>
  68.       <MachineModel>
  69.         <ModelNo>456789</ModelNo>
  70.         <MonitorTypes>
  71.           <MonitorType>
  72.             <MonitorName>17" LCD</MonitorName>
  73.             <ScreenResolution>
  74.               <Resolution>1024*768</Resolution>
  75.               <RefreshRate>60</RefreshRate>
  76.             </ScreenResolution>
  77.             <ScreenResolution>
  78.               <Resolution>800*600</Resolution>
  79.               <RefreshRate>60</RefreshRate>
  80.             </ScreenResolution>
  81.           </MonitorType>
  82.           <MonitorType>
  83.             <MonitorName>19" LCD</MonitorName>
  84.             <ScreenResolution>
  85.               <Resolution>1024*768</Resolution>
  86.               <RefreshRate>60</RefreshRate>
  87.             </ScreenResolution>
  88.             <ScreenResolution>
  89.               <Resolution>800*600</Resolution>
  90.               <RefreshRate>60</RefreshRate>
  91.             </ScreenResolution>
  92.           </MonitorType>
  93.         </MonitorTypes>
  94.       </MachineModel>
  95.       <MachineModel>
  96.         <ModelNo>345678</ModelNo>
  97.         <MonitorTypes>
  98.           <MonitorType>
  99.             <MonitorName>17" CRT</MonitorName>
  100.             <ScreenResolution>
  101.               <Resolution>1024*768</Resolution>
  102.               <RefreshRate>60</RefreshRate>
  103.             </ScreenResolution>
  104.             <ScreenResolution>
  105.               <Resolution>800*600</Resolution>
  106.               <RefreshRate>60</RefreshRate>
  107.             </ScreenResolution>
  108.           </MonitorType>
  109.           <MonitorType>
  110.             <MonitorName>19" CRT</MonitorName>
  111.             <ScreenResolution>
  112.               <Resolution>1024*768</Resolution>
  113.               <RefreshRate>60</RefreshRate>
  114.             </ScreenResolution>
  115.             <ScreenResolution>
  116.               <Resolution>800*600</Resolution>
  117.               <RefreshRate>60</RefreshRate>
  118.             </ScreenResolution>
  119.           </MonitorType>
  120.         </MonitorTypes>
  121.       </MachineModel>
  122.     </MachineModels>
  123.   </CfgType>
  124. </Settings>
  125.  
From the above example xml, we can observe two types of "CfgType"(Ane Justin and Jessica Sis). Under each "CfgType" there are different "MachineModel" and under that "ModelNo" and followed by "ScreenResolution".

Now, the output should be shown as follows on the console window:
Expand|Select|Wrap|Line Numbers
  1. Ane Justin
  2.  
  3.   2345678
  4.     17" LCD
  5.       1024*768
  6.       60
  7.       800*600
  8.       60    
  9.     19" LCD
  10.       1024*768
  11.       60
  12.       800*600
  13.       60
  14.  
  15.   0789456
  16.     17" LCD
  17.       1024*768
  18.       60
  19.       800*600
  20.       60    
  21.     19" LCD
  22.       1024*768
  23.       60
  24.       800*600
  25.       60
  26.  
  27. Jessica Sis
  28.  
  29.   456789
  30.     17" LCD
  31.       1024*768
  32.       60
  33.       800*600
  34.       60    
  35.     19" LCD
  36.       1024*768
  37.       60
  38.       800*600
  39.       60
  40.  
  41.   345678
  42.     17" LCD
  43.       1024*768
  44.       60
  45.       800*600
  46.       60    
  47.     19" LCD
  48.       1024*768
  49.       60
  50.       800*600
  51.       60
  52.  
Could some one please help me in showing the output as show above...
Many Thanks...
Oct 24 '07 #1
Share this Question
Share on Google+
1 Reply


jkmyoung
Expert 100+
P: 2,057
One relatively easy way is to use xslt. The xslt file:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  3.     <xsl:output method="text"/>
  4.     <xsl:variable name="newline">
  5.         <xsl:text>
  6. </xsl:text>
  7.     </xsl:variable>
  8.     <xsl:template match="CfgType">
  9.         <xsl:apply-templates select="Name"/>
  10.         <xsl:value-of select="$newline"/>
  11.         <xsl:apply-templates select="MachineModels/MachineModel"/>
  12.     </xsl:template>
  13.     <xsl:template match="MachineModel">
  14.         <xsl:apply-templates/>
  15.         <xsl:value-of select="$newline"/>
  16.     </xsl:template>
  17.     <xsl:template match="Name">
  18.         <xsl:value-of select="."/>
  19.         <xsl:value-of select="$newline"/>
  20.     </xsl:template>
  21.     <xsl:template match="MonitorType">
  22.         <xsl:apply-templates/>
  23.     </xsl:template>
  24.     <xsl:template match="ModelNo">
  25.         <xsl:text>  </xsl:text><xsl:value-of select="."/>
  26.         <xsl:value-of select="$newline"/>
  27.     </xsl:template>
  28.     <xsl:template match="MonitorName">
  29.         <xsl:text>    </xsl:text><xsl:value-of select="."/>
  30.         <xsl:value-of select="$newline"/>
  31.     </xsl:template>
  32.     <xsl:template match="Resolution|RefreshRate">
  33.         <xsl:text>      </xsl:text><xsl:value-of select="."/>
  34.         <xsl:value-of select="$newline"/>
  35.     </xsl:template>
  36. </xsl:stylesheet>
This file can be run along with your source xml using xalan-c: http://xml.apache.org/xalan-c/
Oct 25 '07 #2

Post your reply

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