【问题标题】: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?我正在使用 Xercesc 来解析 xml 文档。我想知道我将如何使用 xml 值作为我的程序的输入?
【发布时间】:2013-11-18 21:04:05
【问题描述】:

我正在使用 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>

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

【问题讨论】:

    标签: java c++ xml xerces-c


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      看来您需要阅读 Xerces 手册,了解如何从 XML 文件中获取值。

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

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

      【讨论】:

        猜你喜欢
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-28
        • 2011-09-09
        • 1970-01-01
        相关资源
        最近更新 更多