NSXMLParser 列号错误

发布于 2024-11-30 21:47:43 字数 523 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半边脸i 2024-12-07 21:47:43

编辑。两次头痛之前我仍然充满希望。现在我认为最好重新格式化文件以避免奇怪的事情,例如同一行中的元素并进行一些空白清理。但这很奇怪。真是个错误。


嗯,这很奇怪,但无论如何我正在写一个答案。

我正在使用示例 XML 和从 NSXML 获得的一些行/列号进行一些测试。

<?xml version="1.0"?>
<catalog class="something">
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
...

一些日志的形式为

column, line
OPEN/CLOS element

(我对智能调试很菜鸟)

15, 2 (A)
OPEN catalog
12, 3 (B)
OPEN book
14, 4
OPEN author
CLOS author
13, 5
OPEN title
CLOS title
13, 6
OPEN genre
CLOS genre
13, 7
OPEN price
CLOS price
20, 8
OPEN publish_date
CLOS publish_date
19, 9
OPEN description
CLOS description
CLOS book
...

有一个始终有效的公式1,即:

columnPosition = columnNumber - length("<element>")

例如,考虑第二行和 (A) 附近的日志:

<catalog class="something">

我希望 columnPosition 等于 0,事实上:

len("<catalog class>") = 15
0 = 15 - length("<catalog class>")

请注意,无论我在其中写入什么,NSXML 的 columnNumber 仍然是 15 “类”标签,但是当我删除整个标签时,它是 9 。使用以下行:

<catalog>

事实上,我期望 columnPosition 等于 0

length("<catalog>") = 9
0 = 9 - length("<catalog>")

现在,考虑以下行和 (B) 附近的日志:

   <book id="bk101">

我期望 columnNumber 等于3。事实上:

length("<book id>") = 9
3 = 12 - length("<book id>")

嗯,这很奇怪。我认为这不是一个很好的解决方案,但至少它有效。我不能简单地删除字符串开头的空格,因为如果有这样一行,它就会失败:

<catalog class="something"><book id="bk101">

您对此有何看法?我感觉有点菜鸟,但如果没有其他方法,我会检查这个作为接受的答案。我很期待你们的想法。


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.

<?xml version="1.0"?>
<catalog class="something">
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
...

Some logs in the form of

column, line
OPEN/CLOS element

(I'm pretty noob at intelligent debugging)

15, 2 (A)
OPEN catalog
12, 3 (B)
OPEN book
14, 4
OPEN author
CLOS author
13, 5
OPEN title
CLOS title
13, 6
OPEN genre
CLOS genre
13, 7
OPEN price
CLOS price
20, 8
OPEN publish_date
CLOS publish_date
19, 9
OPEN description
CLOS description
CLOS book
...

There is a formula that always works1, that is:

columnPosition = columnNumber - length("<element>")

For example, consider the second line and the log near (A):

<catalog class="something">

I expect that columnPosition equals 0, in fact:

len("<catalog class>") = 15
0 = 15 - length("<catalog class>")

Note that NSXML's columnNumber is still 15 whatever I write inside the "class" tag, but it's 9 when I remove the entire tag. With the following line:

<catalog>

I expect that columnPosition equals 0, in fact:

length("<catalog>") = 9
0 = 9 - length("<catalog>")

Now, consider the following line and the log near (B):

   <book id="bk101">

I'm expecting that columnNumber equals 3. In fact:

length("<book id>") = 9
3 = 12 - length("<book id>")

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:

<catalog class="something"><book id="bk101">

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.

撧情箌佬 2024-12-07 21:47:43

为什么不在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文