Load and parse XML files, and load and parse XML files
Loading and parsing XML file 1. XML format
<? Xml version = "1.0" encoding = "UTF-8"?> Indicates the XML file version. The internal text is encoded.
<Root> indicates the root node.
<CityName> Beijing </CityName> is a node in Beijing. CityName indicates the end name and Beijing node value.
<Item key = "1" value = "A"> </Item> key = "1" is the node attribute, key attribute name, and "1" attribute value.
Note: The XML structure is understood as a layer-by-layer nested tree structure.
<? Xml version = "1.0" encoding = "UTF-8"?> <Root> <systemConfig> <CityName> Beijing </CityName> <CityCode> 201 </CityCode> <ParentCityCode> 0 </ParentCityCode> <areaCode> 010 </areaCode> <AgreementUrl> </AgreementUrl> <IntentionLevel> <Item key = "1" value = "A"> </Item> <Item key = "2" value = "B"> </ item> <Item key = "3" value = "C"> </Item> </IntentionLevel> <ComeChannel> <Item key = "1" value = "newspaper"> </Item> <Item key = "2" value = "magazine"> </Item> </ComeChannel> <BuyCarBudget> <Item key = "1" value = "40-50 thousand"> </Item> <Item key = "2" value = "50-60"> </Item> </BuyCarBudget> <IntentionColor> <Item key = "1" value =" red "> </Item> <Item key =" 2 "value =" yellow "> </Item> </IntentionColor> </systemConfig> </root>
2. XML parsing 1. Configure the XML library (only available after configuration)
// (1) Add the Header file Search path // Header Search Paths->/usr/include/libxml2 // (2) add a binary library // Link library-> lixml2.dylib // (3) add compilation options to the source file //-fno-objc-arc // (4) add the header file // # import "GDataXMLNode. h"
2. Use GData
// 1. load and parse the XML file NSString * path = [[NSBundle mainBundle] pathForResource: @ "xml.txt" ofType: nil]; NSData * data = [[NSData alloc] initWithContentsOfFile: path]; // GDataXMLDocument indicates the XML Document Object // initWithData is initialized using NSData, that is, parsing GDataXMLDocument * doc = [[GDataXMLDocument alloc] initWithData: data options: 0 error: nil]; // 2. obtain the command syntax XPath // CityName path:/root/systemConfig/CityName NSArray * array = [doc nodesForXPath: @ "/root/systemConfig/CityName" error: nil]; GDataXMLElement * element = [array firstObject]; NSLog (@ "name = % @ value = % @", element. name, element. stringValue); // obtain the attributes of the specified node. NSArray * items = [doc nodesForXPath: @ "/root/systemConfig/ComeChannel/Item" error: nil]; GDataXMLElement * item = [items firstObject]; // gets an attribute. The attribute uses GDataXMLElement to represent for (GDataXMLElement * attr in item. attributes) {NSLog (@ "name = % @ value = % @", attr. name, attr. stringValue);} // 4. obtain all nodes with the specified name, regardless of the location. // XPath Syntax: // Item NSArray * allItem = [doc nodesForXPath: @ "// Item" error: nil]; for (GDataXMLElement * e in allItem) {NSLog (@ "name = % @", e. name);} // 5. obtain all attributes of a specified name regardless of the location. // XPath Syntax: // @ value NSArray * allValue = [doc nodesForXPath: @ "// @ value" error: nil]; for (GDataXMLElement * e in allValue) {NSLog (@ "value = % @", e. stringValue);} // 6. traverse the XML file layer by layer // obtain the root node // GDataXMLElement * root = doc. rootElement; // obtain the subnode // root. children // obtain the number of subnodes // root. childrenCount // obtain the child node with the specified name // root elementsForName
3. More detailed XML parsing syntax XPath syntax
Link: http://www.w3school.com.cn/xpath/xpath_syntax.asp