我按照此链接http://www.technical-recipes.com/2014/using-boostproperty_tree/来解析xml。但是如何在不指定特定键的情况下读取整个xml?我尝试了以下代码,但无法处理它,我收到的错误为No such node.
代码:
const std::string XML_PATH1 = "./test1.xml";
#define ROOTTAG "roottag"
boost::property_tree::ptree pt1;
boost::property_tree::read_xml( XML_PATH1, pt1);
BOOST_FOREACH(boost::property_tree::ptree::value_type & node, pt1.get_child(ROOTTAG))
{
std::string tagname = node.first;
tagname += ".";
boost::property_tree::ptree subtree = node.second;
BOOST_FOREACH( boost::property_tree::ptree::value_type & v, subtree.get_child(node.first.data()))
{
//does not enter here
tagname += v.first.data();
tagname += ".";
xmlmap[tagname] = tagvalue;
}
}
必须在第二个循环中指定什么而不是node.first.data()
?
BOOST_FOREACH( boost::property_tree::ptree::value_type & v, subtree.get_child(node.first.data()))
请注意,我必须使用BOOST_FOREACH
本身并使用相同的方法。
我引用了很多网站,但在没有指定特定密钥的情况下无法找到如何读取整个xml。
另外,如何使用上述方法读取多级xml?
答案 0 :(得分:1)
当然,正如我在答案中所说的那样,整个“我想将其压平成地图”看起来是徒劳的。
然而,既然你似乎有意并且无法提出迭代迭代ptree的代码,那么这是一个开始:
<强> Live On Coliru 强>
int main() {
boost::property_tree::ptree pt;
boost::property_tree::read_xml("test.xml", pt);
Flat m;
flatten(pt.get_child("roottag"), m, "DEMO");
BOOST_FOREACH(Flat::value_type const& p, m) {
std::cout << p.first << "\t= '" << p.second << "'\n";
}
}
当你这样称呼时:
DEMO.billTo.<xmlattr>.country = 'US'
DEMO.billTo.city = 'Old Town'
DEMO.billTo.name = 'Robert Smith'
DEMO.billTo.state = 'PA'
DEMO.billTo.street = '8 Oak Avenue'
DEMO.billTo.zip = '95819'
DEMO.comment = 'Hurry, my lawn is going wild!'
DEMO.items.item.<xmlattr>.partNum = '926-AA'
DEMO.items.item.USPrice = '39.98'
DEMO.items.item.comment = 'Confirm this is electric'
DEMO.items.item.productName = 'Baby Monitor'
DEMO.items.item.quantity = '1'
DEMO.items.item.shipDate = '1999-05-21'
DEMO.shipTo.<xmlattr>.country = 'US'
DEMO.shipTo.city = 'Mill Valley'
DEMO.shipTo.name = 'Alice Smith'
DEMO.shipTo.state = 'CA'
DEMO.shipTo.street = '123 Maple Street'
DEMO.shipTo.zip = '90952'
打印例如。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"show"])
{
NSIndexPath *indexPath = [self.tableview indexPathForSelectedRow];
if (_segement.selectedSegmentIndex == 0)
{
EventTableViewController *destViewController = segue.destinationViewController;
destViewController.datais = [self.dataArray objectAtIndex:indexPath.row];
_cellValue = destViewController.datais;
}
}
}
Second Tablviewcontroller FetchData Code:
-(void)addEventToCalendar
{
NSArray *dir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbpath=[NSString stringWithFormat:@"%@/Calendar1.sqlite",[dir lastObject]];
sqlite3 *db;
if(sqlite3_open([dbpath UTF8String], &db)!=SQLITE_OK)
return;
sqlite3_stmt *stmt;
NSString *select=[NSString stringWithFormat:@"select * from Events where EVENT_TYPE='%@'",cellValue];
const char *up=[select UTF8String];
if(sqlite3_prepare_v2(db, up, -1, &stmt, NULL)!=SQLITE_OK)
return;
while (sqlite3_step(stmt)==SQLITE_ROW)
{
NSNumber *eventsid=[NSNumber numberWithInt:(int)sqlite3_column_int(stmt,0)];
NSString *eventname=[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
NSString *eventdate= [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSString *eventtype= [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
NSString *eventmonth= [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
NSString *eventstetus= [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 5)];
NSString *eid= [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(stmt, 6)];
[_eventidArray addObject:eventsid];
[_eventArray addObject:eventname];
[_eventDateArray addObject:eventdate];
[_eventstetusArray addObject:eventstetus];
NSLog(@"%@ %@ %@",eventtype,eventmonth,eid);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
NSLog(@"%@ name array in update",_eventArray);
NSLog(@"%@ date array in upadte",_eventDateArray);
NSLog(@"%@ name array in update",_eventidArray);
NSLog(@"%@ date array in upadte",_eventstetusArray);
}