XPathQuery for ObjC

Introduction

XPathQuery4ObjC is a wrapper class for NSXMLParser to query XML documents such as Web API responses with XPath more easily.

I know that some wrapper classes for Objective-C was already released such as XPathQuery. However, these are not appropriate to use for me because I would like to query more directly using XPath and these wrappers have some bugs 🙁

I have developed other my original DOM parsers which are implemented using some native XML parsers such as Xerese, libxml2 and expat on !MacOSX and iOS platforms, but I decided to use NSXMLParser to implement XPathQuery because I know that it is difficult to support all XML features using the native XML parsers. Please check CyberLinkForC, or CyberLinkForCC and CyberX3DForCC if you want to know other my DOM parsers in more detail.

For example, you can query XML element nodes in Web API responses such as Media RSS using the XPathQuery as the following.

NSURL *rssURL = [NSURL URLWithString:@"http://rss.news.yahoo.com/rss/topstories"];
CGXPathQuery *xpathQuery = [[CGXPathQuery alloc] initWithContentsOfURL:rssURL];

if ([xpathQuery parse] == YES) {
    NSString *title = [xpathQuery valueForXPath:@"/rss/channel/title"];
        ..........
    NSArray *entries = [xpathQuery objectsForXPath:@"/rss/channel/item"];
    for (CGXPathObject *xpathObject in entries) {
        NSString *entryTitle = [xpathObject valueForXPath:@"title"];
            ..........
        NSURL *linkUrl = [NSURL URLWithString:[xpathObject valueForXPath:@"link"]];
            ..........
        NSString *imageUrl = nil;
        CGXPathObject *mediaContent = [xpathObject objectForXPath:@"media:content"];
        if (mediaContent != nil) {
            imageUrl = [NSURL URLWithString:[[mediaContent attributes] valueForKey:@"url"]];
        }
            ..........
    }
}

ctb_java3d_browser

Installation

To use XPathQuery on your XCode project, you have to only add the two files, XPathQuery.h and CGXPathQuety.m, into your project 🙂

Classes

XPathQuery is composed of the following two classes, XPathQuery and CGXPathObject.

CGXPathQuery

XPathQuery is a base class to parse XML documents, you have to initialize with the XML data or the URL. You have to use absolute !XPaths to pass the methods such as objectForXPath:.

@interface CGXPathQuery : NSObject {
}
@property(retain) NSError *parserError;
- (id)initWithContentsOfURL:(NSURL *)url;
- (id)initWithData:(NSData *)data;
- (BOOL)parse;
- (NSArray *)objectsForXPath:(NSString *)absoluteXPath;
- (CGXPathObject *)objectForXPath:(NSString *)absoluteXPath;
- (NSArray *)valuesForXPath:(NSString *)absoluteXPath;
- (NSString *)valueForXPath:(NSString *)absoluteXPath;
@end

CGXPathObject

CGXPathObject is a base object of XPathQuery response. CGXPathObject extends NSDictionary to add some useful methods using a category interface. You have to use relative !XPaths to pass the methods such as objectForXPath:.

#define CGXPathObject NSDictionary
@interface CGXPathObject(CGXPathQuery)
- (NSArray *)children;
- (NSString *)name;
- (NSString *)value;
- (NSDictionary *)attributes;
- (NSArray *)objectsForXPath:(NSString *)relativeXPath;
- (CGXPathObject *)objectForXPath:(NSString *)relativeXPath;
- (NSArray *)valuesForXPath:(NSString *)relativeXPath;
- (NSString *)valueForXPath:(NSString *)relativeXPath;
@end

Limitation

XPathQuery supports only the following simple !XPath specification which is minimal subset of XPath, and it doesn’t support the complex specifications currently.

LocationPath ::= RelativeLocationPath
                 | AbsoluteLocationPath

AbsoluteLocationPath ::= '/' RelativeLocationPath

RelativeLocationPath ::= ElementName
                       | RelativeLocationPath '/' ElementName

ElementName ::= [a-zA-Z0-9]+

Additionally, XPathQuery doesn’t support traversal queries which jumps the parent element. For example, the following code get only a first title element instead of all title elements..

XML document

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" Ö..>
Ö..
<item>
    <title>...</title>
    .....
</item>
<item>
    <title>...</title>
    .....
</item>
</rss>

Inappropriate Code

CGXPathQuery *xpathQuery = [[CGXPathQuery alloc] initWithContentsOfURL:rssURL];
if ([xpathQuery parse] == YES) {
    NSArray *itemTitles = [xpathQuery objectsForXPath:@"/rss/channel/item/title"];
    for (CGXPathObject *titleObject in itemTitles) {
        NSString *title = [titleObject value];
        .....
    }
}

Appropriate Code

To get all title elements normally, use the following steps.

CGXPathQuery *xpathQuery = [[CGXPathQuery alloc] initWithContentsOfURL:rssURL];
if ([xpathQuery parse] == YES) {
    NSArray *items = [xpathQuery objectsForXPath:@"/rss/channel/item"];
    for (CGXPathObject *itemObject in entries) {
        NSString *title = [itemObject valueForXPath:@"title"];
        .....
    }
}

Resources

Repositories

Please see the following pages on GitHub to get the source codes with the examples such as a simple RSS reader 🙂

191 thoughts on “XPathQuery for ObjC

  1. Pingback: Leandro Farland

  2. Pingback: MILF City

  3. Pingback: Madelyn Monroe MILF

  4. Pingback: best-premium-domains

  5. Pingback: Assignment help

  6. Pingback: calendula oil

  7. Pingback: personalized pillow cover

  8. Pingback: Click Here

  9. Pingback: Click Here

  10. Pingback: Click Here

  11. Pingback: Click Here

  12. Pingback: Click Here

  13. Pingback: Click Here

  14. Pingback: Click Here

  15. Pingback: Click Here

  16. Pingback: Click Here

  17. Pingback: Click Here

  18. Pingback: Click Here

  19. Pingback: Click Here

  20. Pingback: Click Here

  21. Pingback: Click Here

  22. Pingback: Click Here

  23. Pingback: Click Here

  24. Pingback: moveit studio

  25. Pingback: spaceros

  26. Pingback: Click Here

  27. Pingback: Click Here

  28. Pingback: Reputation Defenders

  29. Pingback: Click Here

  30. Pingback: Click Here

  31. Pingback: Reputation Defenders

  32. Pingback: Reputation Defenders

  33. Pingback: Reputation Defenders

  34. Pingback: Click Here

  35. Pingback: Click Here

  36. Pingback: Click Here

  37. Pingback: Click Here

  38. Pingback: Click Here

  39. Pingback: Click Here

  40. Pingback: Click Here

  41. Pingback: Click Here

  42. Pingback: Click Here

  43. Pingback: Click Here

  44. Pingback: Click Here

  45. Pingback: Click Here

  46. Pingback: grand rapids teeth whitening

  47. Pingback: grand rapids dentist

  48. Pingback: Click Here

  49. Pingback: Click Here

  50. Pingback: https://gquery.org/

  51. Pingback: Click Here

  52. Pingback: Click Here

  53. Pingback: Click Here

  54. Pingback: 카지노사이트정보

  55. Pingback: Click Here

  56. Pingback: Click Here

  57. Pingback: Click Here

  58. Pingback: Click Here

  59. Pingback: Click Here

  60. Pingback: Click Here

  61. Pingback: Click Here

  62. Pingback: Referral Code

  63. Pingback: Click Here

  64. Pingback: Click Here

  65. Pingback: Click Here

  66. Pingback: Click Here

  67. Pingback: Click Here

  68. Pingback: Click Here

  69. Pingback: Click Here

  70. Pingback: Click Here

  71. Pingback: Click Here

  72. Pingback: Click Here

  73. Pingback: Click Here

  74. Pingback: Click Here

  75. Pingback: Click Here

  76. Pingback: 프로그레시브 잭팟

  77. Pingback: 무료 카지노 게임

  78. Pingback: Do antibiotics make you poop how does plaquenil work

  79. Pingback: domain

  80. Pingback: Do antibiotics weaken your immune system

  81. Pingback: domain-broker

  82. Pingback: Why does a man get erect in the morning

  83. Pingback: What are 6 signs of an asthma attack

  84. Pingback: What is the shortest word in America

  85. Pingback: What is the longest dead person revived Zestril

  86. Pingback: best tech startup books

  87. Pingback: how to choose a cardano stake pool

  88. Pingback: Stromectol 12 mg | What are the top 3 bacterial infections

  89. Pingback: Tadalafil over the counter: What is quality of patient care | ScwCMD.com

  90. Pingback: para q sirve la vidalista 20 sildenafil generic

  91. Pingback: z pack antibiotic side effects Do antibiotics make you tired

  92. Pingback: Was ist das beste Mittel bei Erektionsstorungen wo kann ich kamagra kaufen

  93. Pingback: Google reviews

  94. Pingback: furosemide dosing

  95. Pingback: Comment batir une famille meilleure parapharmacie en ligne 2022

  96. Pingback: Comment Fait-on l'amour a une femme enceinte pharmacie en ligne france livraison belgique

  97. Pingback: What position can you conceive a girl?

  98. Pingback: What is the number one thing a man wants in a woman?

  99. Pingback: What are 3 symptoms of a parasite infection ivermectin stromectol where to buy?

  100. Pingback: reputation defenders

  101. Pingback: Is it my fault my husband has erectile dysfunction?

  102. Pingback: How can I love my wife physically?

  103. Pingback: Can two sperms make a baby?

  104. Pingback: Is it rude to ask someone to take an STD test Hydroxychloroquine 200 mg

  105. Pingback: Which organ is affected first in sepsis zithromax 250mg online

  106. Pingback: Quel couleur represente la famille - dangers du viagra apres 70 ans.

  107. Pingback: How long until antibiotics are out of your system ivermectin for goats

  108. Pingback: What a man should not do in a relationship buy cenforce

  109. Pingback: How long do antibiotics last

  110. Pingback: Is 5 days of antibiotics enough

  111. Pingback: 2023 Books

  112. Pingback: How long does it take for good bacteria to restore after antibiotics

  113. Pingback: What is the strongest natural antibiotic for humans

  114. Pingback: Can I drink Coca Cola while taking antibiotics

  115. Pingback: obituaries

  116. Pingback: rip

  117. Pingback: How many antibiotics can I take in a year

  118. Pingback: cemetery location

  119. Pingback: tombstones

  120. Pingback: Is it OK to eat spicy food while taking antibiotics

  121. Pingback: What antibiotics cause anxiety

  122. Pingback: IRA Empire

  123. Pingback: What are symptoms of too much antibiotics

  124. Pingback: What can replace antibiotics traitement gale stromectol

  125. Pingback: vardenafil pills

  126. Pingback: Do antibiotics improve skin

  127. Pingback: what is levitra for

  128. Pingback: viagra buy online no prescription uk

  129. Pingback: Is Ginger a natural antibiotic

  130. Pingback: Is 5 days of antibiotics enough

  131. Pingback: Can I eat yogurt after antibiotics

  132. Pingback: How many antibiotics per day

  133. Pingback: What is the strongest antibiotic for bacterial infection

  134. Pingback: Can antibiotics do more harm than good

  135. Pingback: Is lemon a natural antibiotic

  136. Pingback: Treblab | beats headphones wireless

  137. Pingback: how to bet on football and win

  138. Pingback: buy stromectol 12 mg online Can amoxicillin make your blood pressure go up?

  139. Pingback: mini speakers bluetooth wireless

  140. Pingback: What are the most common side effects of antibiotics - ivermectin stromectol

  141. Pingback: Chirurgie esthétique Tunisie

  142. Pingback: skullcandy earbuds wireless bluetooth

  143. Pingback: Chirurgie esthétique Tunisie

  144. Pingback: sony headphones wireless bluetooth

  145. Pingback: National Chi Nan University

  146. Pingback: Can you get albuterol over the counter prescriber directions for ventolin

  147. Pingback: What is the first drug of choice for asthma ventolin hfa 90 mcg inhaler

  148. Pingback: Which inhaler is best for breathing problem ventolin inhaler price?

  149. Pingback: What is a heart cough furosemide patient handout?

  150. Pingback: What is the most common inhaler for asthma ventolin inhaler generic name

  151. Pingback: Can I drink water after using an inhaler | $15 ventolin

  152. Pingback: Which inhaler is best for shortness of breath | ventolin hfa coupon

  153. Pingback: Can high blood pressure make you tired and dizzy what is chlorthalidone 25 mg used for

  154. Pingback: Board Of Advisors fue

  155. Pingback: Professional Human Resources Management education

  156. Pingback: Faculty of Commerce and Business Administration

  157. Pingback: Econometrics

  158. Pingback: كلية الاقتصاد

  159. Pingback: Pharmacognos

  160. Pingback: Innovative scientific research

  161. Pingback: علاج الاسنان بالمجان

  162. Pingback: Master's degree in dentistry

  163. Pingback: افضل جامعة في مصر

  164. Pingback: practical training

  165. Pingback: student life at the College of Engineering

  166. Pingback: engineering jobs

  167. Pingback: Faculty Regulations

  168. Pingback: Mission of the Faculty of Computer Science

  169. Pingback: Faculty of Engineering

  170. Pingback: MBA in FUE

  171. Pingback: Entrance exams for future university

  172. Pingback: خطابات توصية لجامعة المستقبل

  173. Pingback: Entrance exams for future university

  174. Pingback: Comment s'appelle celui qui aime faire l'amour viagra vente libre pays

  175. Pingback: Comment s'appelle les trois grandes familles d'instruments acheter du cialis

  176. Pingback: 112 mg synthroid | Dry and hoarse cough that persists for an extended period can be a sign of thyroid deficiency

  177. Pingback: Can cholesterol levels be improved through consuming fermented soy products like tempeh or miso - lipitor price comparison

  178. Pingback: Can I drink alcohol 3 days after antibiotics?

  179. Pingback: Which yogurt has most probiotics?

  180. Pingback: Is it OK to take painkillers with antibiotics?

  181. Pingback: What are 2 problems with antibiotics?

  182. Pingback: Comment bander avec du Coca levitra 20 mg prix

  183. Pingback: Quel est l'animal qui fait le plus lamour | viagra naturel

  184. Pingback: Est-ce que le bebe pleure dans le ventre generique cialis en pharmacie

  185. Pingback: Can walking everyday flatten your stomach

  186. Pingback: Is zinc Good for erectile dysfunction: vidalista 20 price

  187. Pingback: How can I regain erectile strength - vidalista 40

  188. Pingback: Increased susceptibility to infections, such as frequent colds or respiratory illnesses, may indicate an underactive thyroid?

  189. Pingback: What is the success rate of intrauterine insemination (IUI) in treating female infertility?

  190. Pingback: Are there any specific mind-body interventions, such as fertility visualization or positive affirmations, that support women during fertility treatment cycles?

  191. Pingback: What are the benefits of consuming a diet rich in antioxidants like vitamin C and E for heart health