iOS GDataXML--DOM parsing XML

Keywords: iOS C xml Linker network

  1. Use framework GDataXML-HTML Parsing XML
    1). First import the framework file, and then Command+B compile

    Figure 1.png

    resolvent:
    I. click project, and the following interface appears
    Figure 2.png

    Select Build Settings, enter Header Search Paths under Build Settings to search, as shown in the following figure, click All on the right side of Basic, double-click Header Search Paths, and then add / usr/include/libxml2. Compile again after joining.
    Figure 3.png

    II. Error after compilation
    Figure 4.png

    At this time, search Other Linker Flags according to the previous search method, then add - lxm2, and the compilation is successful again.

  2. Get XML data from the network

- (void)loadXML {
    
    NSURL *url = [NSURL URLWithString:@"https://127.0.0.1/videos.xml"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
     ^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
         if (connectionError) {
             NSLog(@"Connection error %@", connectionError);
             return;
         }
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304) {
             // Analytical data
             
         } else {
             NSLog(@"Server internal error");
         }
     }];
}
  1. Complete the data analysis at the comment analysis data in the previous step
// Analytical data
             
             // 1. Load xml document
             GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data error:NULL];
             
             // 2. Get the root element of xml document
             GDataXMLElement *rootElement = document.rootElement;
             
             // Create a variable array to hold objects
             NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
             
             // 3. Traverse all nodes
             for (GDataXMLElement *element in rootElement.children) {
                 // create object
                 Video *v = [[Video alloc] init];
                 // 4. Add objects to the array
                 [array addObject:v];
                 
                 // 5. Assign values to the properties of the object
                 for (GDataXMLElement *subElement in element.children) {
                     [v setValue:subElement.stringValue forKey:subElement.name];
                 }
                 
                 // 6. Traverse all attributes of video
                 for (GDataXMLNode *attr in element.attributes) {
                     [v setValue:attr.stringValue forKey:attr.name];
                 }
                 
             }
             
             NSLog(@"%@", array);
  1. Print results


    Figure 5.png
videos.xml content
<videos>
<video videoId="1">
<name>01.C language-Syntax Preview</name>
<length>320</length>
<videoURL>/itcast/videos/01.C language-Syntax Preview.mp4</videoURL>
<imageURL>/itcast/images/head1.png</imageURL>
<desc>C language-Syntax Preview</desc>
<teacher>Li Lei</teacher>
</video>
<video videoId="2">
<name>02.C language-First C program</name>
<length>2708</length>
<videoURL>/itcast/videos/02.C language-First C program.mp4</videoURL>
<imageURL>/itcast/images/head2.png</imageURL>
<desc>C language-First C program</desc>
<teacher>Li Lei</teacher>
</video>
<video videoId="3">
<name>03.C language-function</name>
<length>822</length>
<videoURL>/itcast/videos/03.C language-function.mp4</videoURL>
<imageURL>/itcast/images/head3.png</imageURL>
<desc>C language-function</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="4">
<name>04.C language-Basic data type</name>
<length>1113</length>
<videoURL>/itcast/videos/04.C language-Basic data type.mp4</videoURL>
<imageURL>/itcast/images/head4.png</imageURL>
<desc>C language-Basic data type</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="5">
<name>05.C language-Basic operation</name>
<length>594</length>
<videoURL>/itcast/videos/05.C language-Basic operation.mp4</videoURL>
<imageURL>/itcast/images/head5.png</imageURL>
<desc>C language-Basic operation</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="6">
<name>06.C language-array</name>
<length>923</length>
<videoURL>/itcast/videos/06.C language-array.mp4</videoURL>
<imageURL>/itcast/images/head6.png</imageURL>
<desc>C language-array</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="7">
<name>07.C language-Character string</name>
<length>848</length>
<videoURL>/itcast/videos/07.C language-Character string.mp4</videoURL>
<imageURL>/itcast/images/head7.png</imageURL>
<desc>C language-Character string</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="8">
<name>08.C language-Pointer</name>
<length>502</length>
<videoURL>/itcast/videos/08.C language-Pointer.mp4</videoURL>
<imageURL>/itcast/images/head8.png</imageURL>
<desc>C language-Pointer</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="9">
<name>09.C language-Pointer use case</name>
<length>558</length>
<videoURL>/itcast/videos/09.C language-Pointer use case.mp4</videoURL>
<imageURL>/itcast/images/head1.png</imageURL>
<desc>C language-Pointer use case</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="10">
<name>10.C language-Pointers and arrays</name>
<length>1682</length>
<videoURL>/itcast/videos/10.C language-Pointers and arrays.mp4</videoURL>
<imageURL>/itcast/images/head2.png</imageURL>
<desc>C language-Pointers and arrays</desc>
<teacher>Mei Mei Han</teacher>
</video>
<video videoId="11">
<name>11.C language-Pointers and strings</name>
<length>1092</length>
<videoURL>/itcast/videos/11.C language-Pointers and strings.mp4</videoURL>
<imageURL>/itcast/images/head3.png</imageURL>
<desc>C language-Pointers and strings</desc>
<teacher>Mei Mei Han</teacher>
</video>
</videos>

Posted by cfgcjm on Wed, 11 Dec 2019 08:11:38 -0800