Does anyone know how to take a XML file and store it into a C++ structure?
-
For example, take: "<"?xml version="1.0" encoding="UTF-8"">" "<"person">" "<"name>Mike"<"/name">" "<"age>12"<"/age">" "<"/person">" "<"ENDOFXML">" and store it into: struct person { string name; int age; } so that the main program would appear as: int main { person a; a.name = "Mike"; a.age = 12; return 0; } Thanks -Mike
-
For example, take: "<"?xml version="1.0" encoding="UTF-8"">" "<"person">" "<"name>Mike"<"/name">" "<"age>12"<"/age">" "<"/person">" "<"ENDOFXML">" and store it into: struct person { string name; int age; } so that the main program would appear as: int main { person a; a.name = "Mike"; a.age = 12; return 0; } Thanks -Mike
-
It should be pretty straightforward to write an XSL stylesheet to do this. The output type of the stylesheet would need to be Text.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
Would you please show me how to do this with my small example? I appreciate your help, -Mike
-
For example, take: "<"?xml version="1.0" encoding="UTF-8"">" "<"person">" "<"name>Mike"<"/name">" "<"age>12"<"/age">" "<"/person">" "<"ENDOFXML">" and store it into: struct person { string name; int age; } so that the main program would appear as: int main { person a; a.name = "Mike"; a.age = 12; return 0; } Thanks -Mike
Here you go. <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" > <xsl:output method="text" indent="yes"/> <xsl:template match="/"> xsl:textint main { </xsl:text> <xsl:apply-templates /> xsl:text return 0; }</xsl:text> </xsl:template> <xsl:template match="person"> xsl:textperson a; a.name = </xsl:text> <xsl:value-of select="name"/> xsl:text; a.age = </xsl:text> <xsl:value-of select="age"/> xsl:text; </xsl:text> </xsl:template> </xsl:stylesheet> Hope it helps.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
Here you go. <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" > <xsl:output method="text" indent="yes"/> <xsl:template match="/"> xsl:textint main { </xsl:text> <xsl:apply-templates /> xsl:text return 0; }</xsl:text> </xsl:template> <xsl:template match="person"> xsl:textperson a; a.name = </xsl:text> <xsl:value-of select="name"/> xsl:text; a.age = </xsl:text> <xsl:value-of select="age"/> xsl:text; </xsl:text> </xsl:template> </xsl:stylesheet> Hope it helps.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
What does the .xml file look like? Doesn't it have to have a reference to the .xsl file? Do both the .xml and the .xsl have to co-exist in the same file? Thanks, Mike
-
What does the .xml file look like? Doesn't it have to have a reference to the .xsl file? Do both the .xml and the .xsl have to co-exist in the same file? Thanks, Mike
Mike A. Fowler wrote:
What does the .xml file look like?
Like the XML fragment you included in your original post, minus the ENDOFXML element.
Mike A. Fowler wrote:
Doesn't it have to have a reference to the .xsl file?
Not necessarily. You can directly link an XML document to an XSL stylesheet by including the following declaration below your XML declaration:
<?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?>
If the XML file is viewed in a browser like IE, the browser will perform the transformation and display the result.
Mike A. Fowler wrote:
Do both the .xml and the .xsl have to co-exist in the same file?
No, the XML and XSL documents reside in separate files. It wouldn't make any sense for the two to be in the same file. The stylesheet would be tightly coupled to a single XML document and couldn't be applied to other documents.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush