ios下最简单的xml ,它就是TBXml.

来源:互联网 时间:1970-01-01

1.解析已知结构的xml。先看下xml的基本结构:<?xml version="1.0"?><authors> <author name="J.K. Rowling"> <book title="Harry Potter and the Philosopher's Stone" price="119.99"> <description>Harry potter thinks he is an ordinary boy - until he is rescued from a beetle-eyed giant of a man, enrolls at Hogwarts School of Witchcraft and Wizardry, learns to play quidditch and does battle in a deadly duel. </description> </book> <book title="Harry Potter and the Chamber of Secrets" price="8.99"> <description>When the Chamber of Secrets is opened again at the Hogwarts School for Witchcraft and Wizardry, second-year student Harry Potter finds himself in danger from a dark power that has once more been released on the school. </description> </book> <book title="Harry Potter and the Prisoner of Azkaban" price="12.99"> <description>Harry Potter, along with his friends, Ron and Hermione, is about to start his third year at Hogwarts School of Witchcraft and Wizardry. Harry can't wait to get back to school after the summer holidays. (Who wouldn't if they lived with the horrible Dursleys?) But when Harry gets to Hogwarts, the atmosphere is tense. There's an escaped mass murderer on the the loose, and the sinister prison guards of Azkaban have been called in to guard the school. </description> </book> </author> <author name="Douglas Adams"> <book title="The Hitchhiker's Guide to the Galaxy" price="15.49"> <description>Join Douglas Adams's hapless hero Arthur Dent as he travels the galaxy with his intrepid pal Ford Prefect, getting into horrible messes and generally wreaking hilarious havoc. </description> </book> <book title="The Restaurant at the End of the Universe " price="14.36"> <description>Arthur and Ford, having survived the destruction of Earth by surreptitiously hitching a ride on a Vogon constructor ship, have been kicked off that ship by its commander. Now they find themselves aboard a stolen Improbability Drive ship commanded by Beeblebrox, ex-president of the Imperial Galactic Government and full-time thief. </description> </book> </author></authors>
//调用- (void)viewDidLoad {tbXml = [TBXML tbxmlWithXMLFile:@"books.xml"];TBXML *tbXml2 = [TBXML tbxmlWithXMLString:@"<root><elem1 attribute1=/"elem1-attribute1/"/><elem2 attribute2=/"attribute2/"/></root>"];TBXML *tbXml3 = [[TBXML alloc] initWithURL:[NSURL URLWithString:@""]]; [self testTBXM:tbXml];[self dealUnknow:tbXml3];[super viewDidLoad];}//解析代码- (void)testTBXM:(TBXML *)tbx { TBXMLElement *root = tbx.rootXMLElement;TBXMLElement *author = [TBXML childElementNamed:@"author" parentElement:root];NSString *name = [TBXML valueOfAttributeNamed:@"name" forElement:author];NSLog(@"author:%@", name);TBXMLElement *book = [TBXML childElementNamed:@"book" parentElement:author];TBXMLElement *descriptionElem = [TBXML childElementNamed:@"description" parentElement:book];NSString * description = [TBXML textForElement:descriptionElem];NSLog(@"author:%@", description); }2.递归解析未知结构的xml。//主调用方法- (void)dealUnknow:(TBXML *)tbx {if (tbx.rootXMLElement) {TBXMLElement *element = tbx.rootXMLElement; [self recurrence:element];}else {NSLog(@"Format Error!");} }//递归子方法- (void)recurrence:(TBXMLElement *)element {do {NSLog(@"<%@>:{%@}",[TBXML elementName:element], [TBXML textForElement:element]);// Display the name of the element//迭代处理所有属性TBXMLAttribute * attribute = element->firstAttribute;while (attribute) {//显示NSLog(@"<%@>->[%@ = %@]", [TBXML elementName:element], [TBXML attributeName:attribute], [TBXML attributeValue:attribute]);//迭代attribute = attribute->next;}//递归处理子树if (element->firstChild) {[self recurrence:element->firstChild];}//迭代处理兄弟树} while ((element = element->nextSibling)); }总之,tbxml解析xml飞速、简洁,可惜不能操作回写,仅陷于解析,做rss巨合适不过了!如想进步了解,请参加下篇,tbxml常用api: