我已经使用TouchXml parser.Its working fine.But实现了演示,我想像下面这样解析xml。
示例:
<Root>
<tag1></tag1>
<tag2>
<temp1></temp1>
<temp2></temp2>
<temp3></temp3>
</tag2>
<tag3></tag3>
</Root>如何解析这种类型的示例?
发布于 2009-12-08 17:32:07
TouchXML很好用,也很容易使用。首先,您需要解析文档:
NSError *theError = NULL;
CXMLDocument *theXMLDocument = [[[CXMLDocument alloc] initWithXMLString:input options:0 error:&theError] autorelease];然后,您可以使用XPath查询文档的结构。例如,要提取Root元素,您可以这样做:
NSArray *foundRoots = [theXMLDocument nodesForXPath:@"//Root" error:&theError];
CXMLElement *root = [foundRoots objectAtIndex:0];(您通常会得到返回的数组,因此在本例中,假设文档中存在第一个元素,则只需获取该元素)
你也可以做一些事情,比如获取一个元素的所有子元素。所以如果我们想要得到所有的标签,我们可以这样做:
NSArray *children = [root children];或者,我们可以获得一个具有特定名称的标记:
NSArray *tag1 = [root elementsForName:@"tag1"];
(同样,您会得到一个数组,所以做正确的事情并进行检查)
您的数据是否符合任何模式?
https://stackoverflow.com/questions/1865393
复制相似问题