0

我是 touchxml 和 xpath 的新手,我只想知道是否有任何方法可以动态获取节点的属性?我有这个xml

<?xml version="1.0" encoding="UTF-8"?>
<countries>
   <country id="Philippines">
      <countryname>Philippines</countryname>
      <subsidiaries>
          <subsidiary>
              <name>Sartorius Philippines Inc.</name>
              <address>Unit 20-A The World Centre Building, 330 Senator Gil Puyat Avenue Makati 1209 City Philippines, Philippines</address>
              <phone>+63.2.8640.929</phone>
              <fax>+63.28640.932</fax>
              <email>enquiry.philippines@sartorius.com</email>
              <website>http://www.sartorius-mechatronics.com.ph</website>
          </subsidiary>
     </subsidiaries>
  </country>
  <country id="Denmark">
       <countryname>Denmark</countryname>
       <subsidiaries>
          <subsidiary>
              <name>Sartorius Stedim Nordic A|S</name>
              <address>stedim Hoerskaetten 6d 2630 Taastrup, Denmark</address>
              <phone>+45.7023.4400</phone>
              <fax>+45.4630.4030</fax>
              <email>ne.customersupport@sartorius.com</email>
              <website></website>
         </subsidiary>
         <subsidiary>
              <name>Sartorius Nordic A|S</name>
              <address>Hoerskaetten 6D 2630 Taastrup, Denmark</address>
              <phone>+45.7023.4400</phone>
              <fax>+45.4630.4030</fax>
              <email>ne.customersupport@sartorius.com</email>
              <website></website>
       </subsidiary>
     </subsidiaries>
    </country>
</countries>`

我有一个显示所有国家/地区名称的表格视图,我希望发生的是当我单击丹麦这个词时,它会显示 id="denmark" 的属性

我现在有这个 xpath 查询:

resultNodes = [rssParser2 nodesForXPath:@"/countries/country[@id]/subsidiaries/subsidiary" error:nil];
4

1 回答 1

1

我不是 100% 确定在 Objective-C 中连接字符串的最佳方法,但我的想法是,假设你有一个countryId带有你正在寻找的 id 的变量,你会做这样的事情:

NSString *xpath =
    [NSString stringWithFormat:@"%@/%@/%@",
         @"/countries/country[@id = '", countryId, @"']/subsidiaries/subsidiary"];
resultNodes = [rssParser2 nodesForXPath:xpath error:nil];
于 2013-01-29T09:54:42.147 回答