如何按照关键字读取xml文件。我现在有一个简单的XML文件,
<?xml version="1.0" standalone="yes"?>
<Catalog Name="china">
<Country Index="1" TypeName="JiangSu">
<VisitFeature>
<Case Name="NanJing" HTTPReturn="401" BasicRealm="zhongshanling"/>
<Case Name="SuZhou" HTTPReturn="200" HTTPReturnDataKeyWords="suzhou"/>
</VisitFeature>
<LoginFeature LoginPage="111" LoginPageKeyWords="321"/>
<TypeFeature TypePage="222" TypePageKeyWords="321"/>
</Country >
<Country Index="2" TypeName="ZheJiang">
<VisitFeature>
<Case Name="Hangzhou" HTTPReturn="401" BasicRealm="xihu"/>
<Case Name="XiaoShan" HTTPReturn="200" HTTPReturnDataKeyWords="xiaoshan"/>
</VisitFeature>
<LoginFeature LoginPage="333" LoginPageKeyWords="456"/>
<TypeFeature TypePage="444" TypePageKeyWords="456"/>
</Country >
<Country Index="3" TypeName="AnHui">
<VisitFeature>
<Case Name="HuangShan" HTTPReturn="401" BasicRealm="hongcun" />
</VisitFeature>
<LoginFeature LoginPage="555" LoginPageKeyWords="789"/>
<TypeFeature TypePage="666" TypePageKeyWords="789"/>
</Country >
</Catalog>
如何只读取index=2当HTTPReturn="401"的BasicRealm的值!结贴后立即给分!

解决方案 »

  1.   

    IXMLDOMNodePtr pNode = pXMLDom->selectSingleNode (
        "//Catalog/Country[@Index='2']/VisitFeature/Case[@HTTPReturn='401']"
    );
    if(pNode)
    {
    _bstr_t nodeVal = pNode->Gettext();
    printf(nodeVal);
    printf("\n");
    }
    else 
    printf("null!\n");IXMLDOMElementPtr pEle = pNode;
    _variant_t v = pEle->getAttribute (_bstr_t("BasicRealm"));
    _bstr_t bstrBasicRealm = _bstr_t(v);
    printf (bstrBasicRealm);
    printf("\n");
      

  2.   

    使用msxml3或4,这里省去了创建COM实例,打开文件,以及善后处理等工作。
    当然了,你这个Case节点没有值,nodeVal是空的,但bstrBasicRealm是可以得到的,我试过了。