简体   繁体   English

在目标C中的两个空XML元素之间解析文本

[英]Parsing Text Between Two Empty XML Elements in Objective C

我已经知道如何解析包含内容的XML元素(在Objective C中为<this> Content </this> ,但是我目前正在使用一个Web服务,该服务返回两个封闭元素之间的所需内容( <begin-paragraph/> The content I need <end-paragraph/> )我一直在网上寻找任何其他这样做的例子,但是我什么也找不到。如果有人知道如何在两个空白元素之间进行阅读并愿意分享,我会非常感谢。

I have to say I regard that as an abuse of XML. 我不得不说我认为这是对XML的滥用。

But I've checked and sadly it is well formed so NSXMLParser (which I assume is what you are using) should be able to cope with it. 但是我已经检查过,令人遗憾的是它的格式正确,因此NSXMLParser(我假设您正在使用的)应该能够应对。

You basically need to check which element you are in by handling the start element and end element events in your NSXMLParserDelegate . 基本上,您需要通过处理NSXMLParserDelegate中的start元素和end元素事件来检查您所在的元素。 Then after receiving the –parser:didEndElement:namespaceURI:qualifiedName: message for begin-paragraph grab all the text you receive in -parser:foundCharacters: until you receive –parser:didStartElement:namespaceURI:qualifiedName:attributes: for end-paragraph 然后,在收到–parser:didEndElement:namespaceURI:qualifiedName:消息的begin-paragraph抓取您在-parser:foundCharacters:收到的所有文本,直到收到–parser:didStartElement:namespaceURI:qualifiedName:attributes:作为end-paragraph

I don't know what the DOM conformance on the iPhone is like, but the general procedure would be: 我不知道iPhone上的DOM一致性如何,但是一般过程如下:

  1. Navigate to <begin-paragraph /> in your DOM. 导航到DOM中的<begin-paragraph />
  2. Get the next sibling of that node. 获取该节点的下一个同级。 That is the content you need. 这就是您需要的内容。 (Node::nextSibling property) (Node :: nextSibling属性)
  3. If there are other elements in there that you want, keep collecting them by the same method, until you reach <end-paragraph /> 如果其中还需要其他元素,请继续使用相同的方法收集它们,直到到达<end-paragraph />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM