繁体   English   中英

使用libxml2解析xml文件时,UNIX中的C ++

[英]C++ in UNIX when parsing an xml file using libxml2

如何通过使用xpath使用libxml2从C ++中的xml文件中检索节点和属性值?

在此先感谢,Bhargava

由于这是用C ++标记的,因此我假设您可以使用libxml ++库绑定。

我写了一个简单的程序:

  • 使用DomParser解析文档
  • 使用文档根节点上的find()进行XPath查询,以获取属性。
  • 将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

这是如何获取要使用libxml ++评估的xpath值的示例。

基于Alexandre Jasmin的答案,但他只展示了如何打印xpath属性,并且弄清楚如何打印Node的值也不是一件容易的事,因为您必须将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;
}

sample.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

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;
}

我通过自己尝试获得了输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM