DOM parsing XMl documents

1, Writing xml files

Put the written XML document on the desktop – > the double of Documents
Verify that the XML document is written correctly on the network at: http://127.0.0.1/ Document name

2, Create a class inherited from NSObject, for example: the class name is Luck
Define the attribute in the Luck.h file [the attribute name is the same as the attribute name in the XML file]

@property (nonatomic ,strong) NSString *name, *like;

3, Create table in ViewController.h
Sign form agreement

<UITableViewDelegate , UITableViewDataSource>
// Define member variables -- table / variable dictionary
{
    NSMutableDictionary *_dic;

    UITableView *_table;
}
// Initialize table
_table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
//Defining macros -- links to XML files
 #Define test uurl @ "http://127.0.0.1/xml document name"
// DOM parsing -- import third-party documents
// Create connection object
NSURL *url = [NSURL URLWithString:TEST_URL];

// Initialize session
NSURLSession *session = [NSURLSession sharedSession];

// Request process
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    // Parsing with DOM
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];

    // Get the contents of the root tag
    GDataXMLElement *rootEle = [doc rootElement];

    // Initialize dictionary
    _dic = [[NSMutableDictionary alloc] init];

    // ergodic
    for (GDataXMLElement *luckEle in [rootEle elementsForName:@"luck"]) {

        // Create a string to get the key of the dictionary
        NSString *key = [[luckEle attributeForName:@"kind"] stringValue];

        // Create array
        NSMutableArray *myArr = [[NSMutableArray alloc] init];

        // Traverse the check tag in the root tag
        for (GDataXMLElement *heroEle in [luckEle elementsForName:@"people"]) {

            // Materialized object
            Luck *myLuck = [[Luck alloc] init];

            // Create an array to get the name tag under the check tag
            NSArray *nameArr = [heroEle elementsForName:@"name"];

            // Get unique value in array
            GDataXMLElement *nameEle = [nameArr firstObject];

            // Get content in tag
            myLuck.name = [nameEle stringValue];

            // Get like tag content
            myLuck.like = [[[heroEle elementsForName:@"like"] firstObject] stringValue];
            // Add to array
            [myArr addObject:myLuck];
        }

        // Set dictionary
        [_dic setObject:myArr forKey:key];
    }

    // Return to main thread refresh table
    dispatch_async(dispatch_get_main_queue(), ^{

       // Refresh table
        [_table reloadData];
    });

}];

// Start request
[task resume];

Set the number of table sections

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dic.count;
}

Set the number of table rows

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [_dic.allKeys objectAtIndex:section];

    return [[_dic objectForKey:key] count];
}

Set cell content

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
    }

    NSString *key = [_dic.allKeys objectAtIndex:indexPath.section];
    cell.textLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] name];
    cell.detailTextLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] like];

    return cell;
}
// Header -- section name
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_dic.allKeys objectAtIndex:section];
}

Tags: xml Attribute Session network

Posted on Mon, 04 May 2020 07:27:34 -0400 by Stevis2002