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"]];
}
..........
}
}
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 🙂
Pingback: Leandro Farland
Pingback: MILF City
Pingback: Madelyn Monroe MILF
Pingback: best-premium-domains
Pingback: Assignment help
Pingback: calendula oil
Pingback: personalized pillow cover
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: moveit studio
Pingback: spaceros
Pingback: Click Here
Pingback: Click Here
Pingback: Reputation Defenders
Pingback: Click Here
Pingback: Click Here
Pingback: Reputation Defenders
Pingback: Reputation Defenders
Pingback: Reputation Defenders
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: grand rapids teeth whitening
Pingback: grand rapids dentist
Pingback: Click Here
Pingback: Click Here
Pingback: https://gquery.org/
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: 카지노사이트정보
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Referral Code
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: Click Here
Pingback: 프로그레시브 잭팟
Pingback: 무료 카지노 게임
Pingback: Do antibiotics make you poop how does plaquenil work
Pingback: domain
Pingback: Do antibiotics weaken your immune system
Pingback: domain-broker
Pingback: Why does a man get erect in the morning
Pingback: What are 6 signs of an asthma attack
Pingback: What is the shortest word in America
Pingback: What is the longest dead person revived Zestril
Pingback: best tech startup books
Pingback: how to choose a cardano stake pool
Pingback: Stromectol 12 mg | What are the top 3 bacterial infections
Pingback: Tadalafil over the counter: What is quality of patient care | ScwCMD.com
Pingback: para q sirve la vidalista 20 sildenafil generic
Pingback: z pack antibiotic side effects Do antibiotics make you tired
Pingback: Was ist das beste Mittel bei Erektionsstorungen wo kann ich kamagra kaufen
Pingback: Google reviews
Pingback: furosemide dosing
Pingback: Comment batir une famille meilleure parapharmacie en ligne 2022
Pingback: Comment Fait-on l'amour a une femme enceinte pharmacie en ligne france livraison belgique
Pingback: What position can you conceive a girl?
Pingback: What is the number one thing a man wants in a woman?
Pingback: What are 3 symptoms of a parasite infection ivermectin stromectol where to buy?
Pingback: reputation defenders
Pingback: Is it my fault my husband has erectile dysfunction?
Pingback: How can I love my wife physically?
Pingback: Can two sperms make a baby?
Pingback: Is it rude to ask someone to take an STD test Hydroxychloroquine 200 mg
Pingback: Which organ is affected first in sepsis zithromax 250mg online
Pingback: Quel couleur represente la famille - dangers du viagra apres 70 ans.
Pingback: How long until antibiotics are out of your system ivermectin for goats
Pingback: What a man should not do in a relationship buy cenforce
Pingback: How long do antibiotics last
Pingback: Is 5 days of antibiotics enough
Pingback: 2023 Books
Pingback: How long does it take for good bacteria to restore after antibiotics
Pingback: What is the strongest natural antibiotic for humans
Pingback: Can I drink Coca Cola while taking antibiotics
Pingback: obituaries
Pingback: rip
Pingback: How many antibiotics can I take in a year
Pingback: cemetery location
Pingback: tombstones
Pingback: Is it OK to eat spicy food while taking antibiotics
Pingback: What antibiotics cause anxiety
Pingback: IRA Empire
Pingback: What are symptoms of too much antibiotics
Pingback: What can replace antibiotics traitement gale stromectol
Pingback: vardenafil pills
Pingback: Do antibiotics improve skin
Pingback: what is levitra for
Pingback: viagra buy online no prescription uk
Pingback: Is Ginger a natural antibiotic
Pingback: Is 5 days of antibiotics enough
Pingback: Can I eat yogurt after antibiotics
Pingback: How many antibiotics per day
Pingback: What is the strongest antibiotic for bacterial infection
Pingback: Can antibiotics do more harm than good
Pingback: Is lemon a natural antibiotic
Pingback: Treblab | beats headphones wireless
Pingback: how to bet on football and win
Pingback: buy stromectol 12 mg online Can amoxicillin make your blood pressure go up?
Pingback: mini speakers bluetooth wireless
Pingback: What are the most common side effects of antibiotics - ivermectin stromectol
Pingback: Chirurgie esthétique Tunisie
Pingback: skullcandy earbuds wireless bluetooth
Pingback: Chirurgie esthétique Tunisie
Pingback: sony headphones wireless bluetooth
Pingback: National Chi Nan University
Pingback: Can you get albuterol over the counter prescriber directions for ventolin
Pingback: What is the first drug of choice for asthma ventolin hfa 90 mcg inhaler
Pingback: Which inhaler is best for breathing problem ventolin inhaler price?
Pingback: What is a heart cough furosemide patient handout?
Pingback: What is the most common inhaler for asthma ventolin inhaler generic name
Pingback: Can I drink water after using an inhaler | $15 ventolin
Pingback: Which inhaler is best for shortness of breath | ventolin hfa coupon
Pingback: Can high blood pressure make you tired and dizzy what is chlorthalidone 25 mg used for
Pingback: Board Of Advisors fue
Pingback: Professional Human Resources Management education
Pingback: Faculty of Commerce and Business Administration
Pingback: Econometrics
Pingback: كلية الاقتصاد
Pingback: Pharmacognos
Pingback: Innovative scientific research
Pingback: علاج الاسنان بالمجان
Pingback: Master's degree in dentistry
Pingback: افضل جامعة في مصر
Pingback: practical training
Pingback: student life at the College of Engineering
Pingback: engineering jobs
Pingback: Faculty Regulations
Pingback: Mission of the Faculty of Computer Science
Pingback: Faculty of Engineering
Pingback: MBA in FUE
Pingback: Entrance exams for future university
Pingback: خطابات توصية لجامعة المستقبل
Pingback: Entrance exams for future university
Pingback: Comment s'appelle celui qui aime faire l'amour viagra vente libre pays
Pingback: Comment s'appelle les trois grandes familles d'instruments acheter du cialis
Pingback: 112 mg synthroid | Dry and hoarse cough that persists for an extended period can be a sign of thyroid deficiency
Pingback: Can cholesterol levels be improved through consuming fermented soy products like tempeh or miso - lipitor price comparison
Pingback: Can I drink alcohol 3 days after antibiotics?
Pingback: Which yogurt has most probiotics?
Pingback: Is it OK to take painkillers with antibiotics?
Pingback: What are 2 problems with antibiotics?
Pingback: Comment bander avec du Coca levitra 20 mg prix
Pingback: Quel est l'animal qui fait le plus lamour | viagra naturel
Pingback: Est-ce que le bebe pleure dans le ventre generique cialis en pharmacie
Pingback: Can walking everyday flatten your stomach
Pingback: Is zinc Good for erectile dysfunction: vidalista 20 price
Pingback: How can I regain erectile strength - vidalista 40
Pingback: Increased susceptibility to infections, such as frequent colds or respiratory illnesses, may indicate an underactive thyroid?
Pingback: What is the success rate of intrauterine insemination (IUI) in treating female infertility?
Pingback: Are there any specific mind-body interventions, such as fertility visualization or positive affirmations, that support women during fertility treatment cycles?
Pingback: What are the benefits of consuming a diet rich in antioxidants like vitamin C and E for heart health