其他分享
首页 > 其他分享> > Pugixml中的xpath遍历

Pugixml中的xpath遍历

作者:互联网

目录

简介

xpath – element

xpath – attribute

xpath – descendants

xpath – parent

xpath – wildcard

xpath – bar

xpath – predicates


简介

Pugixml支持xpath表达式,一个xpath表达式可以在XML文档中定位任何类型的元素节点,以及属性节点。要使用xpath,先简单学习如何使用xpath表达式,建议教程:http://www.tizag.com/xmlTutorial/xpathexpression.php

例如,使用Pugixml时,要定位"amount"子元素的xpath表达式为:/inventory/snack/chips/amount

 

简单说下xpath表达式的注意要点:

  1. 每个元素之间用"/"分隔。
  2. 每个元素均是前一个元素的子元素。
  3. 属性需要以"@"开始。
  4. 可以使用绝对路径,也可以使用相对路径。绝对路径以"/"开始,相对路径以当前节点开始。

xpath – element

即选择单个元素节点语法:/inventory/snack/chips/amount

try
{
    pugi::xpath_query xQuery(L"/inventory/snack/chips/amount");
    if (xQuery)
    {
	pugi::xpath_node_set xNodeSet = xmlDoc.select_node(xQuery);	// xQuery可重复使用
    }
}
catch (pugi::xpath_exception& e)
{
    wcout << e.what() << endl;
}
catch (std::bad_alloc&)
{
    wcout << L"memory allocation fails" << endl;
}

xpath – attribute

即选择单个属性节点语法:/inventory/snack/chips/amount/@supplier

try
{
    pugi::xpath_query xQuery(L"/inventory/snack/chips/amount/@supplier");
    if (xQuery)
    {
	pugi::xpath_node_set xNodeSet = xmlDoc.select_node(xQuery);	// xQuery可重复使用
    }
}
catch (pugi::xpath_exception& e)
{
    wcout << e.what() << endl;
}
catch (std::bad_alloc&)
{
    wcout << L"memory allocation fails" << endl;
}

xpath – descendants

即选择所有子孙节点语法,使用2个"/"符号:/inventory//amount

PS:匹配"inventory"节点下所有名称为"amount"的子孙节点

try
{
    pugi::xpath_query xQuery(L"/inventory//amount");
    if (xQuery && xQueryVar.return_type() == pugi::xpath_type_node_set)
    {
	pugi::xpath_node_set xNodeSet = xmlDoc.select_nodes(xQuery);	// xQuery可重复使用
    }
}
catch (pugi::xpath_exception& e)
{
    wcout << e.what() << endl;
}
catch (std::bad_alloc&)
{
    wcout << L"memory allocation fails" << endl;
}

xpath – parent

即选择节点语法,使用2个".."符号:/inventory//amount/..

PS:匹配"inventory"节点下所有名称为"amount"的子孙节点的父节点

try
{
    pugi::xpath_query xQuery(L"/inventory//amount/..");
    if (xQuery && xQueryVar.return_type() == pugi::xpath_type_node_set)
    {
	pugi::xpath_node_set xNodeSet = xmlDoc.select_nodes(xQuery);	// xQuery可重复使用
    }
}
catch (pugi::xpath_exception& e)
{
    wcout << e.what() << endl;
}
catch (std::bad_alloc&)
{
    wcout << L"memory allocation fails" << endl;
}

xpath – wildcard

PS:匹配"inventory"节点下所有的子节点

PS:匹配"inventory"节点下所有的子节点的再下一级所有子节点(注意,仅包含最后一个*所匹配的节点

try
{
    pugi::xpath_query xQuery(L"/inventory/*/*");
    if (xQuery && xQueryVar.return_type() == pugi::xpath_type_node_set)
    {
	pugi::xpath_node_set xNodeSet = xmlDoc.select_nodes(xQuery);	// xQuery可重复使用
    }
}
catch (pugi::xpath_exception& e)
{
    wcout << e.what() << endl;
}
catch (std::bad_alloc&)
{
    wcout << L"memory allocation fails" << endl;
}

xpath – bar

即使用管道符"|"符号,将两个或多个xpath表达式组合成一个表达式:/inventory/*/* | /inventory/*

PS:匹配"inventory"节点下所有的子节点,以及"inventory"节点下所有的子节点的再下一级所有子节点

try
{
    pugi::xpath_query xQuery(L"/inventory/*/* | /inventory/*");
    if (xQuery && xQueryVar.return_type() == pugi::xpath_type_node_set)
    {
	pugi::xpath_node_set xNodeSet = xmlDoc.select_nodes(xQuery);	// xQuery可重复使用
    }
}
catch (pugi::xpath_exception& e)
{
    wcout << e.what() << endl;
}
catch (std::bad_alloc&)
{
    wcout << L"memory allocation fails" << endl;
}

xpath – predicates

predicates—我不知道应该翻译为什么,但可以知道这是过滤条件。predicates需要位于"[ ]"符号中,作为过滤的条件。

1. 子节点作为条件的语法:

parent[child someTestHere]

例如:inventory/drink/lemonade[amount>15]

PS:匹配满足条件的"lemonade"子节点,条件为"amount"子节点的文本value大于15

2. 属性节点作为条件的语法:

element[element's attribute someTestHere]

例如:inventory/drink/lemonade[@supplier='store']

PS:匹配满足条件的"lemonade"子节点,条件为"lemonade"子节点的"supplier"属性节点值为"strore"

3. 当前节点作为条件的语法:

element[. someTestHere]

例如:inventory/drink/pop/amount[. < 20]

PS:匹配满足条件的"amount"子节点,条件为当前"amount"节点的文本value小于20

try
{
    pugi::xpath_query xQuery(L"inventory/drink/lemonade[@supplier='store']");
    if (xQuery && xQueryVar.return_type() == pugi::xpath_type_node_set)
    {
	pugi::xpath_node_set xNodeSet = xmlDoc.select_nodes(xQuery);	// xQuery可重复使用
    }
}
catch (pugi::xpath_exception& e)
{
    wcout << e.what() << endl;
}
catch (std::bad_alloc&)
{
    wcout << L"memory allocation fails" << endl;
}

 

标签:xpath,遍历,amount,Pugixml,xQuery,pugi,节点,inventory
来源: https://blog.csdn.net/kds0714/article/details/89308915