Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Does anyone know how to take a XML file and store it into a C++ structure?

Does anyone know how to take a XML file and store it into a C++ structure?

Scheduled Pinned Locked Moved XML / XSL
tutorialc++xmlquestionannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike A Fowler
    wrote on last edited by
    #1

    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

    P 2 Replies Last reply
    0
    • M Mike A Fowler

      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

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • P pmarfleet

        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

        M Offline
        M Offline
        Mike A Fowler
        wrote on last edited by
        #3

        Would you please show me how to do this with my small example? I appreciate your help, -Mike

        1 Reply Last reply
        0
        • M Mike A Fowler

          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

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • P pmarfleet

            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

            M Offline
            M Offline
            Mike A Fowler
            wrote on last edited by
            #5

            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

            P 1 Reply Last reply
            0
            • M Mike A Fowler

              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

              P Offline
              P Offline
              pmarfleet
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups