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位就是key1

   DDXMLElement * newsAtt = [[news attributes] firstObject];

   // NSLog(@"---%@---%@---%@",newsAtt.XMLString,newsAtt.stringValue,newsAtt.name);







0 0