NSXMLParser 列号错误
我正在尝试使用 NSXMLParser 解析 XML 文件,但方法 [parser columnNumber]
返回错误值。例如,在我的 .xml 中,我有:
...
<Test><something type="great"><lol>Joy</lol> // Three elements in the same line
...
</something>
</Test>
对于元素“Test”,我得到正确的行:
<Test><something type="great"><lol>Joy</lol>
但列号是“6”。 在同一行中,我得到元素“something”的列号“22”:
"great"><lol>Joy</lol>
这是预期的行为吗?
I am trying to parse an XML file using NSXMLParser, but the method [parser columnNumber]
returns a wrong value. For example, in my .xml I have:
...
<Test><something type="great"><lol>Joy</lol> // Three elements in the same line
...
</something>
</Test>
For the element "Test", I get the correct line:
<Test><something type="great"><lol>Joy</lol>
But the column number is "6".
In the same line, I get the column number "22" for the element "something":
"great"><lol>Joy</lol>
Is this an expected behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑。两次头痛之前我仍然充满希望。现在我认为最好重新格式化文件以避免奇怪的事情,例如同一行中的元素并进行一些空白清理。但这很奇怪。真是个错误。
嗯,这很奇怪,但无论如何我正在写一个答案。
我正在使用示例 XML 和从 NSXML 获得的一些行/列号进行一些测试。
一些日志的形式为
(我对智能调试很菜鸟)
有一个始终有效的公式1,即:
例如,考虑第二行和 (A) 附近的日志:
我希望
columnPosition
等于0
,事实上:请注意,无论我在其中写入什么,NSXML 的
columnNumber
仍然是15
“类”标签,但是当我删除整个标签时,它是9
。使用以下行:事实上,我期望
columnPosition
等于0
:现在,考虑以下行和 (B) 附近的日志:
我期望
columnNumber
等于3
。事实上:嗯,这很奇怪。我认为这不是一个很好的解决方案,但至少它有效。我不能简单地删除字符串开头的空格,因为如果有这样一行,它就会失败:
您对此有何看法?我感觉有点菜鸟,但如果没有其他方法,我会检查这个作为接受的答案。我很期待你们的想法。
1 由于简洁和缺乏意愿而避免了正式证明。
Edit. Two headaches ago I was still hopeful. Now I think it is much better to reformat the file to avoid strange things like elements in the same line and do some whitespace cleaning. But this is strange. What a bug.
Well, this is quite strange, but I'm writing an answer anyway.
I was doing some tests with an example XML and some line/column numbers got from NSXML.
Some logs in the form of
(I'm pretty noob at intelligent debugging)
There is a formula that always works1, that is:
For example, consider the second line and the log near (A):
I expect that
columnPosition
equals0
, in fact:Note that NSXML's
columnNumber
is still15
whatever I write inside the "class" tag, but it's9
when I remove the entire tag. With the following line:I expect that
columnPosition
equals0
, in fact:Now, consider the following line and the log near (B):
I'm expecting that
columnNumber
equals3
. In fact:Well, this is strange. I think this is not an excellent solution, but at least it works. I can't simply remove the whitespace in the beginning of the string, because it fails if there is a line like:
What do you think about this? I'm feeling kinda noob but I'm going to check this one as the accepted answer if there is no other way. I'm looking forward to what you guys think.
1 Avoided formal proof for brevity and lack of will.
为什么不在 startElement 方法中增加 level 并在 endElement 中减少 level ?这样您就可以跟踪嵌套级别
Why don't you increment level in startElement method and decrement in endElement? that way you keep track of nesting level