sistla

Hi..

Can some one please help me how to extract the data values from the following XML file using C++ code.

From the following example, 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".
<code>
< xml version="1.0" encoding="utf-8" >
<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<!-- Configuration Type -->
<CfgType>
<Name>Ane Justin</Name>
<MachineModels>
<MachineModel>
<ModelNo>2345678</ModelNo>
<MonitorTypes>
<MonitorType>
<MonitorName>17" LCD</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
<MonitorType>
<MonitorName>19" LCD</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
</MonitorTypes>
</MachineModel>
<MachineModel>
<ModelNo>0789456</ModelNo>
<MonitorTypes>
<MonitorType>
<MonitorName>17" CRT</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
<MonitorType>
<MonitorName>19" CRT</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
</MonitorTypes>
</MachineModel>
</MachineModels>
</CfgType>

<CfgType>
<Name>Jessica Sis</Name>
<MachineModels>
<MachineModel>
<ModelNo>456789</ModelNo>
<MonitorTypes>
<MonitorType>
<MonitorName>17" LCD</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
<MonitorType>
<MonitorName>19" LCD</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
</MonitorTypes>
</MachineModel>
<MachineModel>
<ModelNo>345678</ModelNo>
<MonitorTypes>
<MonitorType>
<MonitorName>17" CRT</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
<MonitorType>
<MonitorName>19" CRT</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
</MonitorTypes>
</MachineModel>
</MachineModels>
</CfgType>
</Settings>

</code>

Now, the output should be shown as follows on the console window:

Ane Justin

2345678
17" LCD
1024*768
60
800*600
60
19" LCD
1024*768
60
800*600
60

0789456
17" LCD
1024*768
60
800*600
60
19" LCD
1024*768
60
800*600
60

Jessica Sis

456789
17" LCD
1024*768
60
800*600
60
19" LCD
1024*768
60
800*600
60

345678
17" LCD
1024*768
60
800*600
60
19" LCD
1024*768
60
800*600
60

Could some one please help me in showing the output as shown above...
Many Thanks...


Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

Do you want to use managed C++ to write managed .NET application (where you could use the .NET classes like XmlReader) or you want to write a native Win32 application with C++ (where you could use MSXML)






Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

sistla

Sorry forgot to mention that!!

Yes i want to use managed C++(.NET application)





Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

Here is an XSLT stylesheet that dumps the data you are looking for as text. Within the .NET framework 2.0 or later you can apply XSLT transformations using System.Xml.Xsl.XslCompiledTransform:

Code Block

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="lb" select="'&#13;&#10;'"/>
<xsl:param name="sp" select="' '"/>
<xsl:template match="/">
<xsl:apply-templates select="Settings/CfgType"/>
</xsl:template>
<xsl:template match="CfgType">
<xsl:value-of select="concat(Name, $lb)"/>
<xsl:apply-templates select="MachineModels/MachineModel"/>
</xsl:template>
<xsl:template match="MachineModel">
<xsl:value-of select="concat(substring($sp, 1, 2), ModelNo, $lb)"/>
<xsl:apply-templates select="MonitorTypes/MonitorType"/>
</xsl:template>
<xsl:template match="MonitorType">
<xsl:value-of select="concat(substring($sp, 1, 4), MonitorName, $lb)"/>
<xsl:apply-templates select="ScreenResolution"/>
</xsl:template>
<xsl:template match="ScreenResolution">
<xsl:value-of select="concat($sp, Resolution, $lb)"/>
<xsl:value-of select="concat($sp, RefreshRate, $lb)"/>
</xsl:template>
</xsl:stylesheet>






Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

Here is a C++ example using XslCompiledTransform to write to Console:Surpriseut:

Code Block

XslCompiledTransform^ xsltProcessor = gcnew XslCompiledTransform();

xsltProcessor->Load(L"XSLTFile1.xsl");

xsltProcessor->Transform(L"XMLFile1.xml", gcnew XsltArgumentList(), Console::Out);






Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

sistla

Thank you Sooo much for your reply....But i am not able to display anything when i use the above c++ code on the console window..I have even debug it several times..But i could not find the reason..Could you please let me know how to display the output correctly with those C++ code line..Many thanks





Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

The code posted works for me with .NET 2.0 when being used with the XML input you posted saved as XMLFile1.xml and my stylesheet saved as XSLTFile1.xsl.

I am not sure why it does not work for you. Can you be more specific as to what happens when you try the code Do you get any error message(s)






Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

sistla

When i execute the application, the console window pops up and "press any key to continue" message only is shown on the console window.. The desired output is not shown on the Console window..

Clearly nothing is written on the console window!!!

Plz Help me out of this!!!

Thanks a lot!!!





Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

That sounds as if the stylesheet is not outputting anything. Make sure the XML is as you posted it, if you have different elements then that stylesheet doing

Code Block

<xsl:apply-templates select="Settings/CfgType"/>

will simply not find those elements (root element named 'Settings' and child element named 'CfgType') and do nothing.




Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

sistla

Thanks a lot...Actually i have two different formats of XML..One that starts with "Settings/CfgType" and the other is "Settings/Type"..And the rest of parameters in both the XML files is the same...

Once again thanks a lot...U Have saved me a lot of time...Thank you So Much...Thanks





Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

sistla

One more doubt!!!!

how to show new values that are added in the xml file. I would like to allow two new "Value1" and "Value2" for every "MachineModel" So that my output look like as below So could you please let me know how to add these two values in the XSLT stylesheet

Code Block

Ane Justin

2345678
17" LCD
1024*768
60
800*600
60
19" LCD
1024*768
60
800*600
60

100

200

0789456
17" LCD
1024*768
60
800*600
60
19" LCD
1024*768
60
800*600
60

125

212

The xml file that i first provided should also be checked against with its corresponding xsd file:

So to do that what are the exceptions that i should handle Please let me know!!!

The xml file with "Value1" and "Value2" is follows and its corresponding xsd file is also followed:

Code Block

<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XMLFile1.xsd">

<CfgType>
<Name>Ane Justin</Name>
<MachineModels>
<MachineModel>
<ModelNo>2345678</ModelNo>
<MonitorTypes>
<MonitorType>
<MonitorName>17" LCD</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
<MonitorType>
<MonitorName>19" LCD</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
</MonitorTypes>

<Value1>100</Value1>
<Value2>120</Value2>
</MachineModel>
<MachineModel>
<ModelNo>0789456</ModelNo>
<MonitorTypes>
<MonitorType>
<MonitorName>17" CRT</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
<MonitorType>
<MonitorName>19" CRT</MonitorName>
<ScreenResolution>
<Resolution>1024*768</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
<ScreenResolution>
<Resolution>800*600</Resolution>
<RefreshRate>60</RefreshRate>
</ScreenResolution>
</MonitorType>
</MonitorTypes>

<Value1>125</Value1>
<Value2>212</Value2>
</MachineModel>
</MachineModels>
</CfgType>
</Settings>




The XSD for this xml is:

Code Block

< xml version="1.0" encoding="utf-8" >
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Settings">
<xs:complexType>
<xs:sequence>
<xs:element name="CfgType" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="0"/>
<xs:element name="MachineModels" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="MachineModel" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="ModelNo" type="xs:string" minOccurs="0"/>
<xs:element name="MonitorTypes" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="MonitorType" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="MonitorName" type="xs:string" minOccurs="0"/>
<xs:element name="ScreenResolution" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Resolution" type="xs:string" minOccurs="0"/>
<xs:element name="RefreshRate" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Value1" type="xs:string" minOccurs="0"/>
<xs:element name="Value2" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Please let me know your thought!!!!!!!

Thanks again..thanks a lot!!!





Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

sistla wrote:

Thanks a lot...Actually i have two different formats of XML..One that starts with "Settings/CfgType" and the other is "Settings/Type"..And the rest of parameters in both the XML files is the same...

Then you can use

Code Block

<xsl:template match="/">
<xsl:apply-templates select="Settings/*[self::CfgType or self::Type]"/>
</xsl:template>
<xsl:template match="CfgType | Type">
<xsl:value-of select="concat(Name, $lb)"/>
<xsl:apply-templates select="MachineModels/MachineModel"/>
</xsl:template>






Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

sistla

could you please reply to my another question..

Thank You





Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

Here is an adapted stylesheet:

Code Block

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="lb" select="'&#13;&#10;'"/>
<xsl:param name="sp" select="' '"/>
<xsl:template match="/">
<xsl:apply-templates select="Settings/CfgType"/>
</xsl:template>
<xsl:template match="CfgType">
<xsl:value-of select="concat(Name, $lb)"/>
<xsl:apply-templates select="MachineModels/MachineModel"/>
</xsl:template>
<xsl:template match="MachineModel">
<xsl:value-of select="concat(substring($sp, 1, 2), ModelNo, $lb)"/>
<xsl:apply-templates select="MonitorTypes/MonitorType"/>
<xsl:apply-templates select="Value1 | Value2"/>
</xsl:template>
<xsl:template match="MonitorType">
<xsl:value-of select="concat(substring($sp, 1, 4), MonitorName, $lb)"/>
<xsl:apply-templates select="ScreenResolution"/>
</xsl:template>
<xsl:template match="Value1 | Value2">
<xsl:value-of select="concat(substring($sp, 1, 4), ., $lb)"/>
</xsl:template>
<xsl:template match="ScreenResolution">
<xsl:value-of select="concat($sp, Resolution, $lb)"/>
<xsl:value-of select="concat($sp, RefreshRate, $lb)"/>
</xsl:template>
</xsl:stylesheet>






Re: XML and the .NET Framework How to extract xml data values from the given specified format using C++ code

Martin Honnen

As for validating an XML input document against an XSD schema please read http://msdn2.microsoft.com/en-us/library/hdf992b8.aspx.