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

使用LIXML2解析XML文件时UNIX中的C++

  •  0
  • Bhargava  · 技术社区  · 15 年前

    如何使用XPath在LC++中使用LIXML2从XML文件中检索节点和属性值?

    事先谢谢, 巴尔加瓦

    3 回复  |  直到 10 年前
        1
  •  7
  •   Alex Jasmin    15 年前

    由于这是标记C++,所以我假定您可以使用LIbXML++库绑定。

    我写了一个简单的程序:

    • 使用 DomParser
    • 使用 find() 在文档根节点上获取属性。
    • 将xpath结果的第一个节点强制转换为 Attribute 结点
    • 使用获取属性字符串值 get_value()
    • 显示该值

    代码如下:

    #include <iostream>
    #include <libxml++/libxml++.h>
    
    using namespace std;
    using namespace Glib;
    using namespace xmlpp;
    
    int main(int argc, char* argv[])
    {
        // Parse the file
        DomParser parser;
        parser.parse_file("file.xml");
        Node* rootNode = parser.get_document()->get_root_node();
    
        // Xpath query
        NodeSet result = rootNode->find("/root/a/b/@attr");
    
        // Get first node from result
        Node *firstNodeInResult = result.at(0);
        // Cast to Attribute node (dynamic_cast on reference can throw [fail fast])
        Attribute &attribute = dynamic_cast<Attribute&>(*firstNodeInResult);
    
        // Get value of the attribute
        ustring attributeValue = attribute.get_value();
    
        // Print attribute value
        cout << attributeValue << endl;
    }
    

    鉴于此输入:

    <!-- file.xml -->
    <root>
      <a>
        <b attr="I want to get this"> </b>
      </a>
    </root>
    

    代码将输出:

    I want to get this
    

    要在UNIX系统上编译此命令,请执行以下操作:

    c++ `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs` file.cpp
    
        2
  •  1
  •   Alex Jasmin    15 年前

    sp1.xml文件:

    <users noofids="1">
            <user user="vin" password="abc"/>
    </users>
    

    程序:

    #include <libxml/xpath.h>
    #include <libxml/tree.h>
    #include <iostream>
    using namespace std;
    
    int
    main (int argc, char **argv)
    {
      char ID[25];
      xmlInitParser ();
      //LIBXML_TEST_VERSION
      xmlDoc *doc = xmlParseFile ("sp1.xml");
      xmlXPathContext *xpathCtx = xmlXPathNewContext (doc);
      xmlXPathObject *xpathObj =
        xmlXPathEvalExpression ((xmlChar *) "users/user", xpathCtx);
      xmlNode *node = xpathObj->nodesetval->nodeTab[0];
      xmlAttr *attr = node->properties;
      while (attr)
        {
          //if(!xmlStrcmp(attr->name,(const xmlChar *)"noofids"))
          //sprintf(ID,"%s",attr->children->content);
          std::cout << "Attribute name: " << attr->name << "  value: " << attr->
        children->content << std::endl;
          attr = attr->next;
        }
      //std::cout<<"ID: "<<ID<<endl;
      return 0;
    }
    

    我靠自己的努力获得了成果

        3
  •  1
  •   Mike S    10 年前

    这是一个示例,说明如何使用libxml++获取要评估的xpath的值。

    基于Alexandre Jasmin的答案,但他只显示了如何打印xpath属性,并且了解如何打印 Node 的值,因为您必须将其强制转换为特定对象(他的回答也会引发异常)。

    #include <iostream>
    #include <libxml++/libxml++.h>
    
    using namespace std;
    using namespace Glib;
    using namespace xmlpp;
    
    int main(int argc, char* argv[])
    {
        // Parse the file
        DomParser parser;
        parser.parse_file("sample.xml");
        Node* root = parser.get_document()->get_root_node();
    
        // Xpath query
        NodeSet result = root->find("/root/ApplicationSettings/level_three");
    
        // Get first element from result
        Element *first_element = (Element *)result.at(0);
    
        // Print the content of the Element
        cout << first_element->get_child_text()->get_content() << endl;
    }
    

    示例.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <root>
        <ApplicationSettings>
            <level_three>hello world</level_three>
        </ApplicationSettings>
    </root>
    

    编译

    g++ test.cpp -o test `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs`
    

    运行

    ./test
    
    推荐文章