获取 XML 子元素——包括标签名称
我正在尝试在 iPhone 上使用 NSXMLParser
解析一些 XML。
当我检测到所需的元素时,我希望起始元素和结束元素之间的所有内容都为 as is 。
例如,给定:
<A>
<K>
<C>
<D>dddd</D>
</C>
<E> eeee </E>
</K>
<F> ffff </f>
</A>
我想指定 K
并接收:
<C>
<D> dddd </D>
</C>
<E> eeee </E>
但是,当前 NSXMLParser 忽略所有 <
、>
和 属性。 我应该在委托方法中添加什么来确保我获得指定标签之间的所有内容?
这就是我现在所拥有的:
//---when the start of an element is found---
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
if( [elementName isEqualToString:@"ns11:createNewPrincipalResponse"])
{
elementFound = YES;
NSLog(@"PrincResp FOUND");
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound)
{
[parseResults appendString: string];
}
}
//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
if ([elementName isEqualToString:@"ns11:createNewPrincipalResponse"])
{
//---displays the country---
NSLog(@"\n******************\nParse Results...\n*************\n\n%@",parseResults);
[parseResults setString:@""];
token = [NSString stringWithString:parseResults];
elementFound = FALSE;
}
}
提前致谢!
I am trying to parse some XML using NSXMLParser
on the iPhone.
When I detect the element I want, I would like to have everything as is between the start element and end element.
For example, given:
<A>
<K>
<C>
<D>dddd</D>
</C>
<E> eeee </E>
</K>
<F> ffff </f>
</A>
I would like to specify K
And receive:
<C>
<D> dddd </D>
</C>
<E> eeee </E>
However, currently NSXMLParser ignores all the <
, >
, and attributes.
What do I put in the delegate methods to make sure I get everything as is in between the tags I specify?
Here is what I have right now:
//---when the start of an element is found---
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
if( [elementName isEqualToString:@"ns11:createNewPrincipalResponse"])
{
elementFound = YES;
NSLog(@"PrincResp FOUND");
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound)
{
[parseResults appendString: string];
}
}
//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
if ([elementName isEqualToString:@"ns11:createNewPrincipalResponse"])
{
//---displays the country---
NSLog(@"\n******************\nParse Results...\n*************\n\n%@",parseResults);
[parseResults setString:@""];
token = [NSString stringWithString:parseResults];
elementFound = FALSE;
}
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用columnNumber、lineNumber解析器的方法来获取元素开始的位置(即将它们存储为您要查找的元素的didStartElement中的column1、line1),然后当该元素结束时,在didEndElement中再次获取column2和line2然后将原始 XML 中的文本从(行 1,列 1)剪切到(行 2,列 2)。
相当老套,但它可能会起作用。
编辑:要提取文本,您可以按照以下方式执行操作:
我不知道元素开始/结束时解析器的位置是在该元素的开头还是结尾 - 您可能需要自己跳过它,或者,如果位置位于元素的末尾,请检查 column1/column2 是否不在该行之外(在这种情况下,不要添加任何内容)。另外,如果您需要保留换行符,则需要自己添加它们(而不是使用appendString:str使用appendFormat:"%@\n", str)
You can probably use columnNumber, lineNumber parser's methods to get the location where your element starts (i.e. store them as column1, line1 in didStartElement for the element you look for), then when that element ends, in didEndElement, get the column2 and line2 again and then just cut the text from the original XML from (line1, column1) to (line2, column2).
Quite hacky but it'll probably work.
Edit: to extract text you could do something along these lines:
I don't know if the parser's position when the element starts/ends is at the beginning or at the end of that element - you may need to either skip it yourself or, if the position is at the end of the element, check if column1/column2 are not outside the line (in which case just don't add anything). Also, if you need to preserve newline characters, you'll need to add them yourself (instead of appendString:str use appendFormat:"%@\n", str)
NSXmlParser 使用起来相当糟糕。您是否尝试过使用 LibXML 进行解析?然后你可以使用 XPath 来查询 XML,这样更简单一种更加标准的解析 XML 文档的方法。
NSXmlParser is fairly horrible to use. Have you tried using LibXML for parsing instead? Then you can use XPath to query the XML, which is much simpler and a much more standard way of parsing XML documents.