Reputation: 263
I have a XML file might contain thousands of nodes like below.
<?xml version="1.0" encoding="utf-8"?>
<root>
<node>
<id>0</id>
<value>data</value>
</node>
<node>
<id>1</id>
<value>data</value>
</node>
<node>
<id>2</id>
<value>data</value>
</node>
</<root>
Each node has its "id" which guarantees be not duplicated to each other and possibly there are gaps between them. My question is what is most efficient way by using boost to search a certain node by using node's id?
Thanks.
UPDATE This is the way I did
property_tree::wptree ptree; // has been loaded somewhere
auto nodes = ptree.get_child(L"root");
bool bFound = false;
for (auto& itr : nodes)
{
auto & rec = itr.second;
int id = rec.get<int>(L"id", -1);
if (id == nodeId)
{
bFound = true;
// found it
// get other values
break;
}
}
I do not think scanning whole the file to find an item is efficient.
Upvotes: 0
Views: 860
Reputation: 393759
I've used LibXml2 with the TextReader interface.
You can continue doing xmlReaderRead
and checking whether a pattern matches (xmlPatterncompile
and xmlPatternMatch
to see when a node is matched.
You can even get the full "DOM subtree" at that point, so you have the best of both worlds.
CAVEAT: libxml++'s wrapper of
xmlReaderExpand()
and similar accessors¹ are documented to leak memory. I've fixed this in our local code base. I might publish a librarified version of that on github given enough interest and permission.
All in all, this nets the same kind of interface as .NET's XpathReader: What ever happened to XPathReader
¹ so TextReader::expand()
Upvotes: 2