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

解释匹配"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/..

解释匹配"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

  • 即使用通配符"*"符号,表示选择所有子节点:/inventory/*

解释匹配"inventory"节点下所有的子节点

  • 高级应用:/inventory/*/*

解释匹配"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/*

解释匹配"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]

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

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

element[element's attribute someTestHere]

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

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

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

element[. someTestHere]

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

解释匹配满足条件的"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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值