xml解析
2015-12-25 17:57
330 查看
xml文档
<?xml version="1.0" encoding="utf-8" ?>
<root>
<systemConfig>
<CityName>北京</CityName>
<CityName>武汉</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="报纸"></Item>
<Item key="2" value="杂志"></Item>
</ComeChannel>
<BuyCarBudget>
<Item key="1" value="40-50万"></Item>
<Item key="2" value="50-60万"></Item>
</BuyCarBudget>
<IntentionColor>
<Item key="1" value="红"></Item>
<Item key="2" value="黄"></Item>
</IntentionColor>
</systemConfig>
</root>
XML的简单使其易于在任何应用程序中读写数据,这使XML很快成为数据交换的唯一公共语言,虽然不同的应用软件也支持其它的数据交换格式,但不久之后他们都将支持XML,那就意味着程序可以更容易的与Windows,
Mac OS, Linux以及其他平台下产生的信息结合,然后可以很容易加载XML数据到程序中并分析它,并以XML格式输出结果。
找到Header Search Path 把${SDK_ROOT}/usr/include/libxml2添加进去
1某些起始标签可以选择性出现结束标签或者隐含了结束标签。
2某些起始标签要求必须出现结束标签,例如HTLM中<script>“脚本”标签。
3标签可以以任何顺序嵌套。即使结束标签不按照起始标签的逆序出现也是允许的,例如,This is asamplestring是正确的。
某些特性要求必须包含值,例如<图片
源="百度百科.jpg">中的源特性。
4某些特性不要求一定有值,例如中的“不换行”(nowrap)特性。
5定义特性的两边有没有加上双引号都是可以的,所以都是允许的。
NSString*str=[NSString
stringWithContentsOfFile:@"/Users/ms/Desktop/XML备课/XML备课/xml.txt"
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"[===%@",str);
//文档节点
DDXMLDocument * document = [[DDXMLDocument
alloc]initWithXMLString:str
options:0
error:nil];
//获取根节点
DDXMLElement * root = [document
rootElement];
//NSLog(@"root==%@",[root attributes]);
DDXMLElement * systemConfig = [[root
elementsForName:@"systemConfig"]
firstObject];
//获取所有子节点
NSArray*childrenArray=[systemConfig
children];
//获取第一个节点
DDXMLElement*cityName=[childrenArray
firstObject];
NSLog(@"%@~~%@~~%@",cityName.stringValue,cityName.XMLString,cityName.name);
//使用xpath语法获取到所有的item子节点
NSArray*xpathConfigChildrenArray=[systemConfig
nodesForXPath:@"//Item"
error:nil];
for (DDXMLElement*ele
in xpathConfigChildrenArray) {
//获取属性
NSArray*attArray=[ele
attributes];
for (DDXMLElement*attEle
in attArray) {
NSLog(@"Item~~~~%@~~%@~~%@",attEle.stringValue,attEle.XMLString,attEle.name);
}
}
//如果网络回来的数据
NSString* dataxml = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]] encoding:NSUTF8StringEncoding];
NSString * xmlTest = [NSString
stringWithContentsOfFile:XMLPATH
encoding:NSUTF8StringEncoding
error:nil];
//创建document
//取出所有节点
DDXMLDocument * document = [[DDXMLDocument
alloc]initWithXMLString:xmlTest
options:0
error:nil];
//取出节点下的内容
DDXMLElement * root = [document
rootElement];
NSLog(@"------%@------%@------%@",root.XMLString,root.stringValue,root.name);
//取出systemConfig
DDXMLElement * systemConfig = [[root
children] firstObject];
//取出北京
DDXMLElement * cityName = [[systemConfig
children] firstObject];
NSLog(@"---%@---%@---%@",cityName.XMLString,cityName.stringValue,cityName.name);
DDXMLElement * IntentionLevel = [[systemConfig
children] objectAtIndex:6];
NSLog(@"--%@",IntentionLevel);
NSArray * array = [systemConfig
nodesForXPath:@"//Item"
error:nil];
DDXMLElement * news = array[4];
// NSLog(@"---%@---%@---%@",news.XMLString,news.stringValue,news.name);
//取出标签头里面的属性值
//数组取第0位就是key=1
DDXMLElement * newsAtt = [[news
attributes] firstObject];
// NSLog(@"---%@---%@---%@",newsAtt.XMLString,newsAtt.stringValue,newsAtt.name);
<?xml version="1.0" encoding="utf-8" ?>
<root>
<systemConfig>
<CityName>北京</CityName>
<CityName>武汉</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="报纸"></Item>
<Item key="2" value="杂志"></Item>
</ComeChannel>
<BuyCarBudget>
<Item key="1" value="40-50万"></Item>
<Item key="2" value="50-60万"></Item>
</BuyCarBudget>
<IntentionColor>
<Item key="1" value="红"></Item>
<Item key="2" value="黄"></Item>
</IntentionColor>
</systemConfig>
</root>
XML的简单使其易于在任何应用程序中读写数据,这使XML很快成为数据交换的唯一公共语言,虽然不同的应用软件也支持其它的数据交换格式,但不久之后他们都将支持XML,那就意味着程序可以更容易的与Windows,
Mac OS, Linux以及其他平台下产生的信息结合,然后可以很容易加载XML数据到程序中并分析它,并以XML格式输出结果。
找到Header Search Path 把${SDK_ROOT}/usr/include/libxml2添加进去
1某些起始标签可以选择性出现结束标签或者隐含了结束标签。
2某些起始标签要求必须出现结束标签,例如HTLM中<script>“脚本”标签。
3标签可以以任何顺序嵌套。即使结束标签不按照起始标签的逆序出现也是允许的,例如,This is asamplestring是正确的。
某些特性要求必须包含值,例如<图片
源="百度百科.jpg">中的源特性。
4某些特性不要求一定有值,例如中的“不换行”(nowrap)特性。
5定义特性的两边有没有加上双引号都是可以的,所以都是允许的。
NSString*str=[NSString
stringWithContentsOfFile:@"/Users/ms/Desktop/XML备课/XML备课/xml.txt"
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"[===%@",str);
//文档节点
DDXMLDocument * document = [[DDXMLDocument
alloc]initWithXMLString:str
options:0
error:nil];
//获取根节点
DDXMLElement * root = [document
rootElement];
//NSLog(@"root==%@",[root attributes]);
DDXMLElement * systemConfig = [[root
elementsForName:@"systemConfig"]
firstObject];
//获取所有子节点
NSArray*childrenArray=[systemConfig
children];
//获取第一个节点
DDXMLElement*cityName=[childrenArray
firstObject];
NSLog(@"%@~~%@~~%@",cityName.stringValue,cityName.XMLString,cityName.name);
//使用xpath语法获取到所有的item子节点
NSArray*xpathConfigChildrenArray=[systemConfig
nodesForXPath:@"//Item"
error:nil];
for (DDXMLElement*ele
in xpathConfigChildrenArray) {
//获取属性
NSArray*attArray=[ele
attributes];
for (DDXMLElement*attEle
in attArray) {
NSLog(@"Item~~~~%@~~%@~~%@",attEle.stringValue,attEle.XMLString,attEle.name);
}
}
//如果网络回来的数据
NSString* dataxml = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]] encoding:NSUTF8StringEncoding];
NSString * xmlTest = [NSString
stringWithContentsOfFile:XMLPATH
encoding:NSUTF8StringEncoding
error:nil];
//创建document
//取出所有节点
DDXMLDocument * document = [[DDXMLDocument
alloc]initWithXMLString:xmlTest
options:0
error:nil];
//取出节点下的内容
DDXMLElement * root = [document
rootElement];
NSLog(@"------%@------%@------%@",root.XMLString,root.stringValue,root.name);
//取出systemConfig
DDXMLElement * systemConfig = [[root
children] firstObject];
//取出北京
DDXMLElement * cityName = [[systemConfig
children] firstObject];
NSLog(@"---%@---%@---%@",cityName.XMLString,cityName.stringValue,cityName.name);
DDXMLElement * IntentionLevel = [[systemConfig
children] objectAtIndex:6];
NSLog(@"--%@",IntentionLevel);
NSArray * array = [systemConfig
nodesForXPath:@"//Item"
error:nil];
DDXMLElement * news = array[4];
// NSLog(@"---%@---%@---%@",news.XMLString,news.stringValue,news.name);
//取出标签头里面的属性值
//数组取第0位就是key=1
DDXMLElement * newsAtt = [[news
attributes] firstObject];
// NSLog(@"---%@---%@---%@",newsAtt.XMLString,newsAtt.stringValue,newsAtt.name);
相关文章推荐
- 简单的C程序<二>:字符间空格处理
- Spring MVC 4 学习2:XML配置,让Spring M跑起来
- linux centos7 svn server 搭建 支持http访问
- 使用java实现去除各种空格
- python中文编码问题
- popen()和pclose()
- JavaScript权威指南--Chapter5 语句
- CSS禁止选择DIV内的文本
- NSMutableString 常用操作
- NSError
- 大白话系列之C#委托与事件讲解(一)
- Mathematica
- Java中Socket上的Read操作阻塞问题
- Dubbo 回声调用 检测提供者是否正常 操作实例
- MySQL内置函数获取几天前的日期
- 连接
- 测试需求分析
- [转]Class 'Think\Log' not found
- 如何设计测试用例
- Xcode could not find developer disk image