开发者

Parsing an XML file to populate a structure in C /C++

开发者 https://www.devze.com 2022-12-12 07:13 出处:网络
I have the following xml file which has attributes corresponding to a structure. How do i read the content from the XML file and populate the coresponding values in the structure?Please see the XML f

I have the following xml file which has attributes corresponding to a structure.

How do i read the content from the XML file and populate the coresponding values in the structure?Please see the XML file below.

<Struct>
<StructEntry name="presenceNotification">
    <Enum>PRESENCE_NOTIFICATION_UEMOBILITY_UPDATE</Enum>
</StructEntry>
<StructEntry name="ueDirectoryInfo">
    <Struct>  <!-- ueDirectoryInfo  -->
        <StructEntry name="acap_PresenceOptsMask">
            <Int>0</Int>
        </StructEntry>
        <StructEntry name="imsi">
            <Struct>
                <StructEntry name="length">
                   <Int>10</Int>
                </StructEntry>
                <StructEntry name="value">
                   <Sequence>
                       <Int>1</Int>
                       <Int>2</Int>
                       <Int>3</Int>
                       <Int>4</Int>
                       <Int>5</Int>
                       <Int>6</Int>
                       <Int>7</Int>
                       <Int>8</Int>
                       <Int>9</Int>
                       <Int>1</Int>
                   </Sequence>
                </StructEntry>
            </Struct>
        </StructEntry>
        <StructEntry name="lac">
            <Int>3001</Int>
        </StructEntry> 
        <StructEntry name="rac">
            <Int>10</Int>
        </StructEntry> 
        <StructEntry name="attachState">
            <Enum>BAG_UEPRESENCE_CSPSATTACHED<Enum>
        </StructEntry>
        <StructEntry name="tmsi">
            <Int>123456</Int>
        </StructEntry>
        <StructEntry name="currentPTMSI">
            <Int>22113344</Int>
        </StructEntry>
        <StructEntry name="oldPTMSI">
            <Int>44331122</Int>
        </StructEntry>
        <StructEntry name="plmnId">
            <Struct>
                <StructEntry name="mcc">
                    <Sequence>
                        <Int>1</Int>
                        <Int>3</Int>
                        <Int>2</Int>
                    </Sequence> 
                </StructEntry>
                <StructEntry name="mnc">
                    <Struct>
                        <StructEntry name="length">
                            <Int>3<Int>
                        </StructEntry>
                        <StructEntry name="value">
                             <Sequence>
                                 <Int>1</Int>
                                 <Int>2</Int>
                                 <Int>3</Int>
                             </Sequence>开发者_开发百科;
                        </StructEntry> 
                    </Struct>
                </StructEntry>
            </Struct>
         </StructEntry>
         <StructEntry name="psDrxCycleLengthCoefficient">
             <Int>5</Int>
         </StructEntry>
         <StructEntry name="periodicRAUpdateTimer">
             <Int>3</Int>
     </StructEntry>


You can use a library (like tinyXML) to parse your XML file


You could try CodeSynthesis XSD:

an open-source, cross-platform W3C XML Schema to C++ data binding compiler.

To give you a taste of what it's capable of, given this XML file:

<contact>
  <name>John Doe</name>
  <email>j@doe.com</email>
  <phone>555 12345</phone>
</contact>

can produce the following C++ code (using generated classes with a corresponding XSD):

auto_ptr<Contact> c = contact ("c.xml");
cout << c->name () << ", "
     << c->email () << ", "
     << c->phone () << endl;

Basically it's a code generator that will generate the C++ classes and code to load the XML data into those classes.


Use a library such as http://msdn.microsoft.com/en-us/magazine/cc163436.aspx


You can have a look at Xerces. A an open source library for XML Parsing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号