【问题标题】:nodesForXPath returning 0 objectsnodesForXPath 返回 0 个对象
【发布时间】:2013-07-04 09:47:28
【问题描述】:

我在使用 TouchXML 将 XML 字符串中的元素解析为数组时遇到问题。

我已经成功创建了一个 CXMLDocument 对象:

 CXMLDocument *doc = 
[[CXMLDocument alloc] initWithXMLString:responseString options:0 error:&error];

我现在正在尝试获取“车辆”下的所有元素:

NSArray *items = [[doc rootElement] nodesForXPath:@"//Vehicles" error:&error];

这将为数组返回 0 个对象。我究竟做错了什么?

CXMLDocument 输出:

<CXMLDocument 0x21043480 [0x24a64650]> <?xml version="1.0" encoding="utf-8"?>
<System xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://howdoi.org/">
  <Vehicles>
    <VehicleUnit>
      <Plate>2353        </Plate>
      <LastGPStime>2013-07-06 13:02</LastGPStime>
      <LastLatitude>60.35405</LastLatitude>
      <LastLongitude>5.36675</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>177</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2355        </Plate>
      <LastGPStime>2013-07-06 13:02</LastGPStime>
      <LastLatitude>60.35571</LastLatitude>
      <LastLongitude>5.36802</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>84.9</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2356        </Plate>
      <LastGPStime>2013-07-06 13:03</LastGPStime>
      <LastLatitude>60.2893</LastLatitude>
      <LastLongitude>5.22843</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>224.4</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2357        </Plate>
      <LastGPStime>2013-07-06 13:03</LastGPStime>
      <LastLatitude>60.39097</LastLatitude>
      <LastLongitude>5.32535</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>27.4</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2358        </Plate>
      <LastGPStime>2013-07-06 13:03</LastGPStime>
      <LastLatitude>60.39585</LastLatitude>
      <LastLongitude>5.3254</LastLongitude>
      <LastSpeed>16.56</LastSpeed>
      <LastCourse>141.5</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2359        </Plate>
      <LastGPStime>2013-07-06 13:02</LastGPStime>
      <LastLatitude>60.39801</LastLatitude>
      <LastLongitude>5.32231</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>12.8</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2360        </Plate>
      <LastGPStime>2013-07-06 13:02</LastGPStime>
      <LastLatitude>60.35562</LastLatitude>
      <LastLongitude>5.36813</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>65.8</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2361        </Plate>
      <LastGPStime>2013-07-06 13:02</LastGPStime>
      <LastLatitude>60.39897</LastLatitude>
      <LastLongitude>5.32094</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>9.5</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2362        </Plate>
      <LastGPStime>2013-07-06 13:03</LastGPStime>
      <LastLatitude>60.28878</LastLatitude>
      <LastLongitude>5.22882</LastLongitude>
      <LastSpeed>22.68</LastSpeed>
      <LastCourse>106.6</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2363        </Plate>
      <LastGPStime>2013-07-06 13:03</LastGPStime>
      <LastLatitude>60.34393</LastLatitude>
      <LastLongitude>5.33881</LastLongitude>
      <LastSpeed>78.84</LastSpeed>
      <LastCourse>169.4</LastCourse>
    </VehicleUnit>
    <VehicleUnit>
      <Plate>2364        </Plate>
      <LastGPStime>2013-07-06 13:03</LastGPStime>
      <LastLatitude>60.28939</LastLatitude>
      <LastLongitude>5.2284</LastLongitude>
      <LastSpeed>0</LastSpeed>
      <LastCourse>234.3</LastCourse>
    </VehicleUnit>
  </Vehicles>
</System>

【问题讨论】:

    标签: iphone xml-parsing touchxml


    【解决方案1】:

    我得到它的工作,必须使用 namespaceMappings 如下:

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"http://howdoi.org/",
                                  @"howdoi",
                                  nil];
    NSArray *items = [doc nodesForXPath:@"//howdoi:VehicleUnit" namespaceMappings:dict error:nil];
    

    这会将数组设置为 11 个项目,然后让我做我想做的事 :)

    【讨论】:

    • 在您的节点中作为索引路径。车辆和车辆单元。两者都不一样?
    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多