我正在使用 Xercesc 来解析 xml 文档。我想知道我将如何使用 XML 值作为程序的输入?

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?

本文关键字:XML 何使用 输入 程序 Xercesc 文档 xml 想知道      更新时间:2023-10-16

我使用Xercesc来解析xml文档。我想知道如何使用xml值作为程序的输入?

我不想使用"cin",我希望从xml文件

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是:

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

如何在我的简单程序中使用计数值?

基本上需要遵循的步骤如下:

  • 初始化Xerces XMLPlatformUtils
  • 初始化Parser并设置其属性
  • 使用解析器解析xml文件(parser->parseURI(xmlFile);)
  • 一旦解析了文档(DOMDocument),就可以获取根节点,然后使用getChildNodes和getElementsByTagName等函数循环遍历其子节点

您可以从Xerces-c文档中的一些示例开始(http://xerces.apache.org/xerces-c/samples-3.html)。DOMCount示例可能是一个很好的起点。

看起来您需要阅读关于如何从XML文件中获取值的Xerces手册。

从您发布的未注释XML文件中,计数可以是组中的项数,也可以是子组中的项数。

我会循环使用计数或显示它们。还有其他方法可以使用计数值,但我认为您对此不感兴趣。