我必须做一些根本错误的事情。我有一个测试程序设置来读取和显示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选项。我必须误解一些非常基本的东西......
答案 0 :(得分:0)
虽然我不确定我理解你要做什么,但你只是在这个循环中打印线:
for ( pugi::xml_attribute attr = nodeIn.first_attribute() ; attr ; attr = attr.next_attribute() )
此循环仅在节点具有一个或多个属性时执行,因此您没有看到没有属性的节点的任何输出。