123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- //
- // XMLParseManager.m
- // XMLParseDome
- //
- // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- #import "XMLParseManager.h"
- #define dispatch_main_safe_async(block) if ([NSThread isMainThread]) {\
- block();\
- }else{\
- dispatch_async(dispatch_get_main_queue(), ^{\
- block();\
- });\
- }
- @interface XMLParseManager()<NSXMLParserDelegate>
- @property (nonatomic, strong) NSXMLParser *parser;
- @property(nonatomic,copy)ParseXMLCompletion block;
- @property (nonatomic, strong) XPDXMLElement *rootElement;
- @property (nonatomic, strong) XPDXMLElement *currentElement;
- @end
- @implementation XMLParseManager
- -(void)parseXMLWithURL:(NSURL *)url completion:(ParseXMLCompletion)block{
- if (url) {
-
- self.block=block;
-
- __weak typeof(self) weakSelf = self;
- NSInputStream *fileInput=[[NSInputStream alloc]initWithURL:url];
-
- _parser = [[NSXMLParser alloc]initWithStream:fileInput];
- _parser.delegate = self;
- __strong typeof(weakSelf) strongSelf = weakSelf;
- [strongSelf.parser parse];
- if (weakSelf.parser.parserError) {
- dispatch_main_safe_async(^{
-
- if (block) {
- block(NO,nil,strongSelf.parser.parserError);
- }
- });
- }
- }
- }
- -(void)parserDidStartDocument:(NSXMLParser *)parser{
- self.rootElement=nil;
- self.currentElement = nil;
- }
- -(void)parserDidEndDocument:(NSXMLParser *)parser{
- __weak typeof(self) weakSelf = self;
- if (self&&self.block) {
-
- dispatch_main_safe_async(^{
- __strong typeof(weakSelf) strongSelf = weakSelf;
- strongSelf.block(YES, strongSelf.rootElement, nil);
- });
- }
- }
- -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
- if (!self.rootElement) {
- self.rootElement = [[XPDXMLElement alloc]init];
- self.currentElement = self.rootElement;
- }else{
- XPDXMLElement *element = [[XPDXMLElement alloc]init];
- element.parent = self.currentElement;
- [self.currentElement.childElement addObject:element];
- element.name = elementName;
- self.currentElement = element;
- }
- self.currentElement.name = elementName;
- self.currentElement.attribute = attributeDict;
- }
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
- {
- [self.currentElement.text appendString:string];
- }
- -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
- self.currentElement = self.currentElement.parent;
-
- }
- - (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validationError{
- if (self.block) {
- __weak typeof(self) weakSelf = self;
- dispatch_main_safe_async(^{
- __strong typeof(weakSelf) strongSelf = weakSelf;
- strongSelf.block(NO, nil,validationError);
- });
- }
- }
- @end
|