代码之家  ›  专栏  ›  技术社区  ›  Alex

为什么libxml2在元素名上输出“text”(而不是“text”)?

  •  2
  • Alex  · 技术社区  · 14 年前

    我正在使用libxml2进行XML解析。libxml网站上的示例代码很难理解,似乎缺少一些细节。我通过google找到了下面的代码,所以我甚至不认为这是正确的方法,但它在我编写的示例学习程序中起了作用,但不是这个。我仍然不知道在C++中使用LIXML的正确方法,所以我在黑暗中运行,希望我能找到一些有用的东西。

    root->name 是的,但是当它通过子对象时,它只是输出 text cur->name 我不知道为什么。我必须把计数器放在那里,防止它进入无限循环。我读到XML文件中的某个空白可能会导致这种情况,但我不知道该怎么办。我只需要零件名称和ID。

    xmlNode *cur = root;
    cur = cur->xmlChildrenNode;
    
    ofstream out;
    out.open("errorlog.txt", ios::app);
    out << "attempting reading current node\n";
    out << "root: " << root->name << endl;
    
    int counter = 0;
    
    // advance until it hits stars
    while(cur != NULL && counter < 10){
    if ((!xmlStrcmp(cur->name, (const xmlChar *)"parts")))
        break;
    
        cur->next;
        counter++;
    }
    
    out << "counter: " << counter << endl;
    out << "child: " << cur->name << endl;
    

    这是我正在使用的XML文件:

    <?xml version="1.0" encoding="utf-8"?>
    <netlist>
        <parts>
            <part name="part10">
                <attribute name="id">1</attribute>
            </part>
            <part name="part20">
                <attribute name="id">2</attribute>
            </part>
            <part name="part30">
                <attribute name="id">3</attribute>
            </part>
        </parts>
    
        <junk>
            <stuff id="3" />
            <stuff id="4" />
            <stuff id="5" />
        </junk>
    </netlist>
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   D.Shawley    14 年前

    问题是您没有推进当前节点 while 循环。尝试改变 cur->next; cur = cur->next; <netlist> <parts>

        2
  •  5
  •   txk2048 anilam    5 年前

    xmlKeepBlanksDefault(0) 忽略空白。