0

libxml2 (2-2.7.8) と xpath (v1) を使用して、C プログラムで wunderground.com が生成した XML ファイルから情報を抽出しています。すべてのノードに情報が含まれている場合、すべてうまく機能します。問題は、一部のノードがデータを欠落している場合です。たとえば、UV データが利用できない場合、wunderground は次のような構文を適用します。

<visibility_km>10.0</visibility_km>
<solarradiation>0.1</solarradiation>
<UV/>
<precip_1hr_string>0.00 in ( 0 mm)</precip_1hr_string>

XPath がナビゲーションと情報の取得を停止する原因となります。私の出力は切り捨てられ、その後のすべてのデータ

<UV/>

失せろ。プログラム全体の実行も停止すると思います。それは正常な行動ですか?ノードが欠落している間に矛盾を回避し、少なくともプログラムの実行を終了するにはどうすればよいですか?

助けてくれてありがとう、最高

ジョバンニ

私のXPath式:

//current_observation/display_location/full | //current_observation/display_location/country_iso3166 | //current_observation/display_location/elevation | //current_observation/observation_location/full | //current_observation/local_time_rfc822 | //current_observation/weather | //current_observation/temp_c | //current_observation/relative_humidity |//current_observation/wind_dir | //current_observation/wind_kph | //current_observation/pressure_mb | //current_observation/feelslike_c | //visibility_km | //current_observation/UV | //current_observation/precip_today_metric | //simpleforecast/forecastdays/forecastday/date/day | //simpleforecast/forecastdays/forecastday/date/monthname | //simpleforecast/forecastdays/forecastday/date/weekday_short | //simpleforecast/forecastdays/forecastday/date/high/celsius | //simpleforecast/forecastdays/forecastday/low/celsius | //simpleforecast/forecastdays/forecastday/date/conditions | //simpleforecast/forecastdays/forecastday/pop | //simpleforecast/forecastdays/forecastday/avewind/kph | //simpleforecast/forecastdays/forecastday/avehumidity

私のコードの一部:

//code

xmlXPathObjectPtr getnodeset (xmlDocPtr doc, xmlChar *xpath, char* thisnames) {
    xmlXPathContextPtr context;
    xmlXPathObjectPtr result;
    context = xmlXPathNewContext(doc);
    if (context == NULL) {
        printf("Error in xmlXPathNewContext\n");
        return NULL;    }

    if(xmlXPathRegisterNs(context,  BAD_CAST "new", BAD_CAST thisnames) != 0) {
    printf("Error: unable to register NS with prefix");
    return NULL;    }


    result = xmlXPathEvalExpression(xpath, context);
    xmlXPathFreeContext(context);
    if (result == NULL) {
        printf("Error in xmlXPathEvalExpression\n");
        return NULL;    }
    if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
        xmlXPathFreeObject(result);
        printf("No result\n");
        return NULL;    }
    return result;
}

//code

doc = xmlParseDoc(chunk.memory);
if (doc == NULL ) {
        printf("Document not parsed successfully. \n");
        return state;   }
result = getnodeset (doc, xpath, thisnames);
if (result) {
    nodeset = result->nodesetval;
    *myarray = malloc((nodeset->nodeNr + 1) * sizeof(*myarray));
    for (i=0; i < nodeset->nodeNr; i++) {
        keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
        (*myarray)[i] = malloc(strlen(keyword)+1);
        if ((*myarray)[i] == NULL) {
        // out of memory.  print error msg then exit
        }
        strcpy((*myarray)[i], keyword);
        printf("id: %d keyword: %s\n",i,keyword);
        xmlFree(keyword);
    }
    xmlXPathFreeObject (result);
    state = i;
}

//code
4

1 に答える 1