0
A
回答
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
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
相關問題
- 1. libxml2 HTML解析
- 2. xml libxml2解析
- 3. C/C++,libxml2:解析HTML片段
- 4. 使用Python中的libxml2解析XMLString
- 5. 使用libxml2基於soap的xml解析
- 6. 簡單的libxml2與Obj-C,Xcode和libxml2的XML解析示例?
- 7. libxml2 HTML塊解析
- 8. 使用libxml2解析xml文件
- 9. 無法使用C中的Libxml2解析XML
- 10. 簡單的libxml2 HTML解析示例,使用Objective-c,Xcode和HTMLparser.h
- 11. 是否有可用的在線LIBXML2 XML解析器或使用libxml2獨立解析XML的方法?
- 12. libxml2的HTML解析問題
- 13. libxml2 xml解析問題
- 14. LibXML2 Sax解析和符號
- 15. 使用libxml2解析存儲在NSData中的XML
- 16. 使用Libxml2解析XML時的大內存使用情況
- 17. 用libxml2解析一個XMPP流
- 18. 的libxml2拒絕解析XML實體
- 19. libxml2的XML DOM解析與給XPathQuery
- 20. 解析C語言中的HTML文件 - libxml2的替代品
- 21. 在Netbeans中使用C++中的libXml2
- 22. 錯誤在C++中使用的libxml2
- 23. 如何使用libxml2解析來自GInputStream(GLibGIO/GVFS)的數據?
- 24. 有關如何使用libXml2解析的參考
- 25. XML解析導致重複使用的libxml2
- 26. 使用unix終端解析XML
- 27. 使用Unix解析和打印$ PATH
- 28. 使用libxml2在C中調用Webservice
- 29. C Unix - 解析一行字符
- 30. 解析使用C
你看到提供的libxml的網站上的例子嗎? – DumbCoder 2010-11-14 11:26:42