Я анализирую xml с использованием библиотеки xerces c ++ и пытаюсь сохранить некоторые теги и значения, которые мне требуются.
У меня есть динамически распределенный массив символов, в котором хранятся фактические значения из проанализированного XML, позже эти значения будут пустыми / отсутствующими?
Примечание: я использую компилятор C ++, я намерен написать как можно больше кода в C.
См. Соответствующие фрагменты основной функции (обратите внимание на важность walk_DOM () & get_data ()).
int main(int argc, char *args[])
{
//pass in as console argument
char *xmlFile = args[1];
file_observational_area *_file_observational_area = NULL;
//initialize file_observational_area
_file_observational_area = calloc_file_observational_area(_file_observational_area, 1);
/*xerces irrelevant code was not included in post as xerces is working*/
parser->parse(xmlFile);
DOMDocument *dom = parser->getDocument();
DOMNodeList *dom_node_list = dom->getChildNodes();
for (unsigned int i = 0; i < dom_node_list->getLength(); i++)
{
walk_DOM(dom_node_list->item(i), _file_observational_area);
}
get_data(_file_observational_area, xmlFile, 0, 0, 0, _field_data_values_array);
}
Заголовок для поля field_delilmited:
#
ifndef FIELD_DELIMITED_H
#define FIELD_DELIMITED_H
# ifdef __cplusplus
extern "C"
{#
endif
struct field_delimited
{
char *name;
char *data_type;
char *unit;
char *description;
};
typedef struct field_delimited field_delimited;
/*=============================================================================
Function correct_column_headers
Description: Are all the headers read from data from the same as described
* in the XML?
References:
Inputs:
Output: 0 for false &1 for true
==========================================================*/
int correct_column_headers(field_delimited *_field_delimited, int expected_number_of_columns, char *delimiter, char *read_header);
#
ifdef __cplusplus
}#
endif
# endif /*FIELD_DELIMITED_H */
См. Соответствующий фрагмент parse_dom_PDS4 () , это функция, которая устанавливает значения изначально в _file_observational_area- >_table_delimited->_record_delimited->_field_delimited
, однако позже значения становятся пустыми при повторении в getData ():
file_observational_area* parse_dom_PDS4(DOMNode *dom_node, file_observational_area *_file_observational_area)
{
/*<Field_Delimited>*/
if (XMLString::equals(dom_node_name, XMLString::transcode("Field_Delimited")))
{
/*<Field_Delimited>
<name>enc4</name>
<data_type>ASCII_Real</data_type>
<unit>rad</unit>
<description>Joint 4 encoder counts[RA_DOF]</description>
</Field_Delimited>*/
/*<Field_Delimited>*/
XMLCh *tag_Field_Delimited_name = XMLString::transcode("name");
string tag_Field_Delimited_name_val;
XMLCh *tag_Field_Delimited_data_type = XMLString::transcode("data_type");
string tag_Field_Delimited_data_type_val;
XMLCh *tag_Field_Delimited_unit = XMLString::transcode("unit");
string tag_Field_Delimited_unit_val;
XMLCh *tag_Field_Delimited_description = XMLString::transcode("description");
string tag_Field_Delimited_description_val;
DOMElement *currentElement = NULL;
cout << "<Field_Delimited> DETECTED\n";
int number_of_fields = _file_observational_area->_table_delimited->_record_delimited->fields;
_file_observational_area->_table_delimited->_record_delimited->_field_delimited = malloc_field_delimited(_file_observational_area->_table_delimited->_record_delimited->_field_delimited, number_of_fields);
if (dom_node->hasChildNodes())
{
// get Record_Delimited children
dom_node_list = dom_node->getChildNodes();
if (NULL != dom_node_list)
{
/*<Field_Delimited> start loop*/
for (unsigned int Field_Delimited_index = 0; Field_Delimited_index < number_of_fields; Field_Delimited_index++)
{
if (NULL != dom_node_list->item(Field_Delimited_index))
{
currentElement = dynamic_cast<xercesc::DOMElement*> (dom_node_list->item(Field_Delimited_index));
if (NULL != currentElement && NULL != currentElement->getNodeName())
{
if (XMLString::equals(tag_Field_Delimited_name, currentElement->getNodeName()))
{
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].name = XMLString::transcode(currentElement->getTextContent());
cout << "Debug: tag_Field_Delimited_name_val:" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].name << "\n";
}
if (XMLString::equals(tag_Field_Delimited_data_type, currentElement->getNodeName()))
{
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].data_type = XMLString::transcode(currentElement->getTextContent());
cout << "Debug: tag_Field_Delimited_data_type_val:" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].data_type << "\n";
}
if (XMLString::equals(tag_Field_Delimited_unit, currentElement->getNodeName()))
{
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].unit = XMLString::transcode(currentElement->getTextContent());
cout << "Debug: tag_Field_Delimited_unit_val:" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].unit << "\n";
}
if (XMLString::equals(tag_Field_Delimited_description, currentElement->getNodeName()))
{
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].description = XMLString::transcode(currentElement->getTextContent());
cout << "Debug: tag_Field_Delimited_description_val:" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[Field_Delimited_index].description << "\n";
}
}
} //end item is not empty
} /*<Field_Delimited> end loop */
} //end dom node list is not empty
} //end has children
} //end /*<Field_Delimited>*/
//other code follows but in irrelevant
}
Позже я пересекаю _file_observational_area
и другие значения там, как и ожидалось, однако значения в _file_observational_area->_table_delimited->_record_delimited->_field_delimited
отсутствуют / пустые ??
См. Соответствующий фрагмент ниже, значения теперь пусты
void get_data(file_observational_area *_file_observational_area, char *pds4_xml_path, int dimensions, int *start_indices, int *end_indices, field_data_value *field_data_values)
{
//number of fields
if (_file_observational_area->_table_delimited->_record_delimited->fields > 0)
{
//allocate memory for actual data
int field_data_value_memory_size = _file_observational_area->_table_delimited->records *_file_observational_area->_table_delimited->_record_delimited->fields;
field_data_values = (field_data_value*) malloc(field_data_value_memory_size* sizeof(field_data_value));
// _file_observational_area->_table_delimited->_record_delimited->_field_delimited
int row_count = 0;
buffer_size = _file_observational_area->_table_delimited->offset;
for (int field_record_index = 0; field_record_index < _file_observational_area->_table_delimited->_record_delimited->fields; field_record_index++)
{
if (NULL != _file_observational_area->_table_delimited->_record_delimited->_field_delimited)
{
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].name;
cout << "name :" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].name << "\n";
//datatype
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].data_type;
cout << "data_type:" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].data_type << "\n";
//unit
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].unit;
cout << "unit:" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].unit << "\n";
//unit
//description
_file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].description;
cout << "description:" << _file_observational_area->_table_delimited->_record_delimited->_field_delimited[field_record_index].description << "\n";
//TODO: check _file_observational_area->_table_delimited->field_delimiter
//TODO:// split character_buffer based on _file_observational_area->_table_delimited->field_delimiter
//TODO: output another file for testing, later data structure for querying
}
}
//other code follows but in irrelevant
}
Заголовок для file_observational.h:
#include <stdlib.h>
#include "data_file.h"
#include "header.h"
#include "table_delimited.h"
#include "record_delimited.h"
#include "field_delimited.h"
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMNode.hpp>
#ifndef FILE_OBSERVATIONAL__H
#define FILE_OBSERVATIONAL__H
struct file_observational_area
{
data_file * _file;
header * _header;
table_delimited * _table_delimited;
};
typedef struct file_observational_area file_observational_area;
/*=============================================================================
Function calloc_file_observational_area
Description: Allocates memory for file_observational_area and children
References:
Inputs:
Output:
==========================================================*/
file_observational_area *
calloc_file_observational_area (file_observational_area * _file_observational_area, int MAX_SIZE);
/*=============================================================================
Function destroy_file_observational_area
Description: free allocated memory for file_observational_area and children
References:
Inputs:
Output:
==========================================================*/
void
destroy_file_observational_area (file_observational_area * _file_observational_area);
/*=============================================================================
Function parse_dom_PDS4
Description: parse based on PDS4 tags
References:
Inputs:
Output:
==========================================================*/
field_delimited * malloc_field_delimited(field_delimited * _field_delimited, int number_of_fields);
/*=============================================================================
Function free_field_delimited
Description: free allocated memory for _record_delimited->_field_delimited
* dynamic array
References:
Inputs:
Output:
==========================================================*/
void
free_field_delimited(field_delimited * _field_delimited, int number_of_fields);
//file_observational_area * parse_dom_PDS4(DOMNode * dom_node,
// file_observational_area * _file_observational_area);
/*=============================================================================
Function malloc_field_delimited
Description: allocated memory for _record_delimited->_field_delimited
* dynamic array
References:
Inputs:
Output:
==========================================================*/
#endif /* FILE_OBSERVATIONAL__H */
Соответствующая часть вывода после запуска программы:
DETECTED
, tag_Field_Delimited_name_val: grapple_phase
, tag_Field_Delimited_data_type_val: ASCII_Integer
, tag_Field_Delimited_unit_val: нет
* 1044_объект * получен data () full_file_path: /home/anaim/sample_pds4_files/for_ary/A0087_0008_597249118_597252671_181130002623_eu.csv
_file_observational_area -> заголовок -> текстовый_объект Заголовки столбцов
ТАБЛИЦА
запись с разделителями
имя:
тип_данных:
единица:
описание:
name: grapple_phase
data_type:
unit:
description:
name:
data_type:
единица:
описание:
имя:
тип_данных: ASCII_Integer
единица:
описание:
name:
data_type:
unit:
description:
Как видно, имена выводятся изначально, например, tag_Field_Delimited_name_val:Ft
, но позже становятся пустыми при итерации в функции get_data (). Почему текстовые значения исчезли ??