我一定是在做一些根本错误的事情。我设置了一个测试程序来读取和显示 xml 文件的内容,以便我可以研究和了解数据是如何存储和表示的。我有巨大的电子表格生成的 xml 文件来驱动我们的装配设备。最终,我需要在这些文件中重新排序、编辑和创建数据。
现在我只是想弄清楚为什么 PUGIXML 似乎忽略了节点数据和没有属性的节点。
这是我在很明显我没有在真实文件中找到所有内容时创建的示例 xml 文件:
<Node1 attr11="attribute11"
attr12="attribute12"
attr13="attribute13"
attr14="attribute14"
attr15="attribute15"
attr16="attribute16">
<Node11 attr111="attribute111">
<Value1>value1</Value1>
<Value2>value2</Value2>
<Value3>value3</Value3>
<Value4>value4</Value4>
</Node11>
<Node12/>
<Node13></Node13>
<Node14 attr141="attribute141"
attr142="attribute142"
attr143="attribute143"
attr144="attribute144">
<Value1>value1</Value1>
<Value2>value2</Value2>
<Value3>value3</Value3>
<Value4>value4</Value4>
</Node14>
<Node15>
<Value1>value1</Value1>
<Value2>value2</Value2>
<Value3>value3</Value3>
<Value4>value4</Value4>
</Node15>
</Node1>
以下是我为与现有代码兼容而围绕 PUGIXML 创建的 C++ 包装器的部分列表:
class XMLFileImporter2
{
public:
// constructor
XMLFileImporter2( string strIn )
{
// stash the file name
m_strTheFileName = strIn;
// make the call to the pugi system
pugi::xml_parse_result result = m_xmlTheXML.load_file( m_strTheFileName.c_str() , pugi::parse_full );
m_strLastResultMessage = string( result.description() );
// did the xml read correctly?
m_bDidFileRead = ( m_strLastResultMessage == "No error" );
....
}
void test1( vector<string> &lstText )
{
// clear the vector
lstText.resize( 0 );
// interate through the xml data
for ( pugi::xml_node thisnode = m_xmlTheXML.first_child() ; thisnode ; thisnode = thisnode.next_sibling() )
{
testRecurse( thisnode , lstText , 0 );
}
}
void testRecurse( pugi::xml_node nodeIn , vector<string> &lstText , int iLevelIn )
{
// first, increment a local copy of the level
int iLocalLevel = iLevelIn + 1;
// temp vars
string strTemp , str1 , str2 , str3 , str4 , str5;
int iAttr = 0;
int iloop;
// build the attribute list for THIS node
for ( pugi::xml_attribute attr = nodeIn.first_attribute() ; attr ; attr = attr.next_attribute() )
{
// increment valid attribute count
iAttr++;
// init the string
strTemp = CUtility::makeStringFromInt( iLocalLevel ) + " : " + CUtility::makeStringFromInt( iAttr ) + " : ";
str1 = string( attr.name() );
str2 = string( attr.value() );
str3 = string( nodeIn.name() );
pugi::xml_node_type tNodeType = nodeIn.type();
str5 = getNodeType( tNodeType );
str4 = string( nodeIn.value() );
if ( str4 == "" ) str4 = "<blank>";
strTemp = strTemp + " Node Name = " + str3 + " Node Type = " + str5 + " Node Val = " + str4 + " Attr Name = " + str1 + " Attr Val = " + str2;
for ( iloop = 0 ; iloop < iLevelIn ; iloop++ )
strTemp = " " + strTemp;
// stash it
lstText.push_back( strTemp );
}
// recurse to any child nodes
for ( pugi::xml_node nextnode = nodeIn.first_child() ; nextnode ; nextnode = nextnode.next_sibling() )
{
testRecurse( nextnode , lstText , iLocalLevel );
}
}
...
};
当我创建包装器的实例时,它会导致成员 PUGIXML 文档加载文件。这似乎在没有任何错误的情况下发生。在我知道这是真的之后,我执行包装器的成员测试遍历方法将整个节点树结构转储到 STL 字符串向量,并将其转储到未排序的列表框。这是我的列表框显示的内容:
1 : 1 : Node Name = Node1 Node Type = node_element Node Val = <blank> Attr Name = attr11 Attr Val = attribute11
1 : 2 : Node Name = Node1 Node Type = node_element Node Val = <blank> Attr Name = attr12 Attr Val = attribute12
1 : 3 : Node Name = Node1 Node Type = node_element Node Val = <blank> Attr Name = attr13 Attr Val = attribute13
1 : 4 : Node Name = Node1 Node Type = node_element Node Val = <blank> Attr Name = attr14 Attr Val = attribute14
1 : 5 : Node Name = Node1 Node Type = node_element Node Val = <blank> Attr Name = attr15 Attr Val = attribute15
1 : 6 : Node Name = Node1 Node Type = node_element Node Val = <blank> Attr Name = attr16 Attr Val = attribute16
2 : 1 : Node Name = Node11 Node Type = node_element Node Val = <blank> Attr Name = attr111 Attr Val = attribute111
2 : 1 : Node Name = Node14 Node Type = node_element Node Val = <blank> Attr Name = attr141 Attr Val = attribute141
2 : 2 : Node Name = Node14 Node Type = node_element Node Val = <blank> Attr Name = attr142 Attr Val = attribute142
2 : 3 : Node Name = Node14 Node Type = node_element Node Val = <blank> Attr Name = attr143 Attr Val = attribute143
2 : 4 : Node Name = Node14 Node Type = node_element Node Val = <blank> Attr Name = attr144 Attr Val = attribute144
任何空节点和任何没有任何显式属性的节点似乎都会丢失。我已经调用了 parse_full load_file 选项。我必须在这里误解一些非常基本的东西......