2010-11-14 68 views
0

如何使用的libxml2通過使用XPath來檢索節點和C來自XML文件的屬性值++的XML文件時?C++在UNIX解析使用的libxml2

由於提前, 巴爾加瓦

+1

你看到提供的libxml的網站上的例子嗎? – DumbCoder 2010-11-14 11:26:42

回答

1

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

我通過嘗試獲得輸出荷蘭國際集團由我自己

7

由於這是標記C++我假設你可以使用的libxml ++庫綁定。

我寫了一個簡單的程序:

  • 解析使用DomParser
  • 使文檔根節點上使用find()去的屬性XPath查詢該文檔。
  • 鑄的XPath結果的第一個節點使用get_value()
  • 顯示看重

這裏的Attribute節點

  • 獲取該屬性字符串值的代碼:

    #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 
    
  • +0

    thans for reply,但我需要同樣的東西只在libxml2 – Bhargava 2011-02-02 07:35:31

    +1

    我可能是錯誤的,但不是版本2的libxml ++ libxml2的包裝?所以實際上這是libxml2。有什麼理由不能使用包裝? – daramarak 2011-02-24 10:26:59

    +0

    我不知道有關包裝,所以我沒有使用它,我可以知道使用包裝? – Bhargava 2013-12-19 10:38:22

    1

    這是如何獲得你想用的libxml ++評估的XPath值的例子。

    基於亞歷山大·茉莉的答案,但他只顯示瞭如何打印的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; 
    } 
    

    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