The class for parsing XML data in iOS is nsxmlparseR, which is detailed using the following methods:
- Suppose you now have an XML binary data object (NSData) in Memory: data (which may be from a network or local file data), set the protocol for NSXMLParseR objects, with the following code:
NSXMLParser *xmlparse = [[Nsxmlparser alloc] initwithdata:data];
[Xmlparse setdelegate:self];
[Xmlparse parse];// parsing begins
- after parsing the object settings, we need to implement the relevant proxy protocol: nsxmlparserdelegate. To implement the Proxy method in this protocol, there are several necessary methods:
parsing begins, looking for elements that need to be parsed
-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict
{
TODO: Gets the value of the address element property of value
if ([@ "Address" isequaltostring:elementname])
{
Self.xmlelementname = elementname;
if (attributedict && [attributedict objectforkey:@ "value"])
{
Self.xmlparseresult = [nsmutablestring string];
[Self.xmlparseresult appendformat:@ "%@", [attributedict objectforkey:@ "value"];
}
}
}
-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string
{
if (!self.xmlelementname)
{
Return
}
}
parse an element end
-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qName
{
if ([@ "Address" isequaltostring:elementname])
{
Self.xmlelementname = nil;
}
}
parse the completion of the document, where the results of the resolution can be processed
-(void) Parserdidenddocument: (Nsxmlparser *) parser
{
Todo:xml parsing complete, results stored in Self.xmlparseresult
if (!self.xmlparseresult | | [@ "" isEqualToString:self.xmlParseResult])
{
The analytical results can be further processed here
}
}
"!--endfragment-->