I'm using Xercesc for parsing an xml document. i wanted to know how i'm going to use the xml values as an input for my program?

StackOverflow https://stackoverflow.com/questions/20057859

  •  31-07-2022
  •  | 
  •  

Question

I'm using Xercesc for parsing an xml document. i wanted to know how i'm going to use the xml values as an input for my program?

I don't want to use the "cin" i want the values to be parsed from the xml file

include "stdafx.h"

#include <iostream>

using namespace std;


void Print(int count, int countSub, int rolePerGroup, int userCount, int userPerGroup)
{
for(int roleCount = 1; roleCount<=rolePerGroup; roleCount ++)
{ 
    if(userPerGroup == 0) 
    {
        cout<<"Parent groups are: "<< count <<" | "<<"Sub group are :     "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl;
        continue;
    }

    for(userCount = 1; userCount<=userPerGroup; userCount ++)
        cout<<"Parent groups are: "<< count <<" | "<<"Sub group are : "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl;
}
}

int main()
{ 
int userCount = 0;
int roleCount = 0;
int parentGroup;
cout<<"enter a number of parentGroup"<< endl;
cin>> parentGroup;

if (parentGroup == 0) 
{ 
    cout<<"Parent Group should not be zero"<<endl;
    exit(EXIT_FAILURE);
}

int subGroup;
cout<<"enter a number sub Group"<< endl;
cin>> subGroup;
int rolePerGroup;
cout<<"enter a number role per Sub Group"<< endl;
cin>> rolePerGroup;

if (rolePerGroup == 0)
{
    cout<<"Role per Group should not be zero"<<endl;
    exit(EXIT_FAILURE);
}

int userPerGroup;
cout<<"enter a number user per Role"<< endl;
cin>> userPerGroup;


for(int count=1;count <= parentGroup; count ++)
{
    if(subGroup == 0) 
    {
        Print( count, 0, rolePerGroup, userCount, userPerGroup);
        continue;
    }

    for(int countSub = 1;countSub<=subGroup; countSub ++)
    { 
        Print( count, countSub, rolePerGroup, userCount, userPerGroup);
    }
}
}

Xml I wanted to use for simple parsing is:

<organizationFile>
    <ParentGroup>
        <Count>15</Count>
        <SubGroup>
            <Count>3</Count>
            <Role>
                <Count>15</Count>
                <User>
                    <Count>3</Count>
                </User>
          </Role>
        </SubGroup>
    </Group>
</organizationFile>

How can i use the count values in my simple program?

Was it helpful?

Solution

Basically the steps you need to follow are the following:

  • Initialise Xerces XMLPlatformUtils
  • Initialise a Parser and set its properties
  • Use the parser to parse your xml file (parser->parseURI(xmlFile);)
  • Once you've got the document parsed (DOMDocument), you can get the root node and then loop through its children nodes with functions like getChildNodes and getElementsByTagName.

You can start by looking at some of the examples in Xerces-c documentation (http://xerces.apache.org/xerces-c/samples-3.html). The DOMCount example could be a good starting point.

OTHER TIPS

Looks like you need to read the Xerces manual on how to fetch the values from the XML file.

From the uncommented XML file you posted, the counts could either be the number of items in the group or number of items in the subgroups.

I would use the counts in a loop or display them. There are other ways to use the count values, but I don't think you are interested in them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top