从iphone中的数组中的xml文件中获取零对象

时间:2011-05-19 12:36:49

标签: iphone xml parsing xpath namespaces

大家好我正在尝试将xpathQuery用于以下xml数据。

我的xml文件以assessmentItem开头。在assessmentItem

之前甚至没有单个字符
<assessmentItem adaptive="false" identifier="wordsearch" timeDependent="false" title="Word Search" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 
http://www.imsglobal.org/xsd/imsqti_v2p1.xsd" 
xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">



<itemBody>


<div id="heading">
  <p><![CDATA[<TextFlow columnCount="inherit" columnGap="inherit" columnWidth="inherit" fontFamily="Verdana" fontSize="9" lineBreak="inherit" paddingBottom="inherit" paddingLeft="2" paddingRight="2" paddingTop="6" verticalAlign="inherit" whiteSpaceCollapse="preserve" xmlns="http://ns.adobe.com/textLayout/2008"><p><span>Word search</span></p></TextFlow>]]></p>
</div>
<div id="direction">
  <p><![CDATA[<TextFlow columnCount="inherit" columnGap="inherit" columnWidth="inherit" fontFamily="Verdana" fontSize="9" lineBreak="inherit" paddingBottom="inherit" paddingLeft="2" paddingRight="2" paddingTop="6" verticalAlign="inherit" whiteSpaceCollapse="preserve" xmlns="http://ns.adobe.com/textLayout/2008"><p><span>Find the words in the grid</span></p></TextFlow>]]></p>
</div>
<div id="mask">
  <p><![CDATA[Yes]]></p>
</div>
<div id="layout">
  <p><![CDATA[13*13]]></p>
</div>
<div id="numOfGuesses">
  <p><![CDATA[10]]></p>
</div>
<matchInteraction maxAssociations="2" responseIdentifier="RESPONSE" shuffle="true">
  <simpleMatchSet>
    <simpleAssociableChoice identifier="W_1" matchMax="1">
      <p><![CDATA[harsha]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_2" matchMax="1">
      <p><![CDATA[dinakar]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_3" matchMax="1">
      <p><![CDATA[hurixsystems]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_4" matchMax="1">
      <p><![CDATA[aspire]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_5" matchMax="1">
      <p><![CDATA[india]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_6" matchMax="1">
      <p><![CDATA[kitab]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_7" matchMax="1">
      <p><![CDATA[google]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_8" matchMax="1">
      <p><![CDATA[australia]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_9" matchMax="1">
      <p><![CDATA[elephant]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_10" matchMax="1">
      <p><![CDATA[mumbai]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_11" matchMax="1">
      <p><![CDATA[hyderabad]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_12" matchMax="1">
      <p><![CDATA[chennai]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_13" matchMax="1">
      <p><![CDATA[dictera]]></p>
    </simpleAssociableChoice>
  </simpleMatchSet>
</matchInteraction>
//bla bla..

形成上面的xml我试图获取包含“p-tag”值的数组作为simpleMatchSet的子元素。 我的xpath查询就像这样

  NSString *xmlFilePath=[[NSBundle mainBundle]pathForResource:@"wordsearchmain" ofType:@"xml"];
NSData *data=[[NSData alloc]initWithContentsOfFile:xmlFilePath];

NSString *xpathQuery = [[NSString alloc] initWithFormat:@"assessmentItem/itemBody/matchInteraction/simpleMatchSet/../simpleAssociableChoice/p/text()"];
NSDictionary *results = PerformXMLXPathQuery(data, xpathQuery);
[xpathQuery release];

我不知道我在哪里错误地从xml.can获取数据任何人都建议我更正我的xpathQuery.Thanks all。所有建议都可接受。

1 个答案:

答案 0 :(得分:1)

您尝试在XPath查询中匹配的元素位于URI为“http://www.imsglobal.org/xsd/imsqti_v2p1”的命名空间中。这是因为在顶级元素上有一个默认的名称空间声明:

xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" 

要匹配这些元素,您有两种选择:

  1. 为“http://www.imsglobal.org/xsd/imsqti_v2p1”定义名称空间前缀,然后在每个元素名称之前在XPath表达式中使用该前缀,例如: "imsq:assessmentItem/imsq:itemBody/imsq:matchInteraction/imsq:simpleMatchSet/imsq:simpleAssociableChoice/imsq:p/text()"

  2. 将您的XPath表达式修改为名称空间不敏感,例如

    "//*[local-name() = 'simpleAssociableChoice']//text()"