CData not being parsed with NSXmlparser

╄→尐↘猪︶ㄣ 提交于 2019-12-24 08:58:09

问题


I receive response in the form of xml from a service,which i parse using nsxmlparser. In an instance i received an xml where the content is same in two tags.(duplicate item as in below xml)

Xml received:

<UserGeneratedContentItem>
    <link
      rel="details"
      uri="http://api.test.no/test.Ugc/vivo/usergeneratedcontentitem/16046" />
    <Data><![CDATA[<Type>Note</Type><Description>**6000 characters**</Description><Page>6</Page><ChapterNumber>1</ChapterNumber><ChapterTitle>Etikk og filosofi</ChapterTitle><Uri>http://api.test.no/test.Ugc/vivo/usergeneratedcontentitem/6</Uri><Data><StickyNotes><StickyNote name="icon_2" X="215.8" Y="352.7" note="**6000 characters**" dateTime="null" popupX="940" popupY="119" popupWidth="300" popupHeight="180" formatedDate ="2012-12-28-18-20-47" iconImage="1003"/></StickyNotes></Data>]]></Data>
  </UserGeneratedContentItem>
  <UserGeneratedContentItem>
    <link
      rel="details"
      uri="http://api.test.no/test.Ugc/vivo/usergeneratedcontentitem/16046" />
    <Data><![CDATA[<Type>Note</Type><Description>**6000 characters**</Description><Page>6</Page><ChapterNumber>1</ChapterNumber><ChapterTitle>Etikk og filosofi</ChapterTitle><Uri>http://api.test.no/test.Ugc/vivo/usergeneratedcontentitem/6</Uri><Data><StickyNotes><StickyNote name="icon_2" X="215.8" Y="352.7" note="**6000 characters**" dateTime="null" popupX="940" popupY="119" popupWidth="300" popupHeight="180" formatedDate ="2012-12-28-18-20-47" iconImage="1003"/></StickyNotes></Data>]]></Data>
  </UserGeneratedContentItem>

6000 characters include alphabet,arithmatic & general punctuation marks which would be encoded.

Prob:

In -(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock method content in CDATABlock variable is not same in both cases(two tags).in one instance it gives me all 6000 characters and in the other instance it gives me only few may be around 300 characters.The text (6000 characters) is same in both cases which i confirmed.

What could be the reason? Can someone help me may be anybody who faced such issue earlier like me.

Tnx in advance.Plz let me know if something is not clear or not understood.


回答1:


thats what cdata is for -- it shouldnt be parsed

CDATA - (Unparsed) Character Data

src: http://www.w3schools.com/xml/xml_cdata.asp

PCDATA should be parsed


you gotta save the block of NSData (may be called N times, so you have to concat that stuff) and -in your case- then parse the result as separate XML




回答2:


Simply call the following function along with other NSXMLParser function. It will then start taking care of CDATA block.

-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{    
    NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];

}

You can NSLog someString, and it will show you the contents of CDATA block.



来源:https://stackoverflow.com/questions/14070750/cdata-not-being-parsed-with-nsxmlparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!