xml解析
来源:互联网 发布:电脑做账软件 编辑:程序博客网 时间:2024/06/05 20:09
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=[NSStringstringWithContentsOfFile:@"/Users/ms/Desktop/XML备课/XML备课/xml.txt"encoding:NSUTF8StringEncodingerror:nil];
NSLog(@"[===%@",str);
//文档节点
DDXMLDocument * document = [[DDXMLDocumentalloc]initWithXMLString:stroptions: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=[systemConfignodesForXPath:@"//Item"error:nil];
for (DDXMLElement*elein xpathConfigChildrenArray) {
//获取属性
NSArray*attArray=[ele attributes];
for (DDXMLElement*attElein attArray) {
NSLog(@"Item~~~~%@~~%@~~%@",attEle.stringValue,attEle.XMLString,attEle.name);
}
}
//如果网络回来的数据
NSString* dataxml = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]] encoding:NSUTF8StringEncoding];
NSString * xmlTest = [NSStringstringWithContentsOfFile:XMLPATHencoding:NSUTF8StringEncodingerror:nil];
//创建document
//取出所有节点
DDXMLDocument * document = [[DDXMLDocumentalloc]initWithXMLString:xmlTestoptions: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 = [[systemConfigchildren] 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解析
- xml解析
- XML解析
- 解析XML
- xml解析
- 解析xml
- 解析xml
- xml解析
- XML解析
- xml解析
- XML解析
- xml解析
- xml解析
- xml解析
- xml解析
- xml解析
- 解析xml
- XML解析
- python中文编码问题
- Java基础-学习笔记(前言)
- 使用java实现去除各种空格
- JavaScript权威指南--Chapter5 语句
- 简单的C程序<二>:字符间空格处理
- xml解析
- Webrtc Intro - kurento
- OC UIFont字体设置
- mongodb格式化结果
- java中的内部类总结
- Duplicate id, tag null, or parent id 0x0 with another fragment 解决方案
- 线程绑定CPU核-sched_setaffinity
- mac编译Android版本ffmpeg的x86_64架构库
- 懒加载、plist文件数据加载、图片显示