我正在实现一个语法阅读器作为家庭作业的副产品,并且我正在一次读取一个字符的 XML 文件。我的目标是利用我通过实验室作业的代码,并改进结构和设计。
我有一个继承自 PushbackReader
的 SyntaxReader
,它有一些额外的方法,例如 readAndExpect(char c)
、trimWhitespace()和<代码>readUntilChar(char c)。然后我有一个继承自 SyntaxReader 的 XMLSyntaxReader 并添加了 readAttribute() 和 readNode() 等功能(后者对于树中的子级是递归的......)。
我基本上只是从我的实验室作业中获取工作代码并将其粘贴到此方法中的适当位置。但是,当我尝试读取第一个子项时,我收到了一个我无法弄清楚的 IOException: Pushback buffer Overflow 。
这是我的 XML 文件的开头:
<Biosfar namn="Liv">ar allt som fortplantar sig
<Rike namn="Vaxter"> etc...
我已经调试并观察了语法读取器成功地读取了子标记开头的 。此时,XMLSyntaxReader
确认它是一个子标记,未读取最后两个字符(R
和 <
,按此顺序)并调用readNode()
来解析子节点。当我尝试取消读取 <
时,会发生异常。
在调试器中,我还验证了 PushbackReader
的私有 pos
变量确实设置为 0
,但我无法弄清楚为什么。我已经解析了一整行文本 - 为什么这些字符仍然不算数?
我会发布我的代码,但它相当冗长,并且在我不确定我的确切实现是否相关之前同样有效。如果是,请指出我可以上传片段的地方,我将分享所有代码。
I'm implementing a syntax reader as a spin-off from a homework assignment, and I'm reading an XML file one character at a time. My goal is to take the code that I passed the lab assignment with, and improve the structure and design.
I have a SyntaxReader
that inherits from PushbackReader
, which has some extra methods like readAndExpect(char c)
, trimWhitespace()
and readUntilChar(char c)
. Then I have an XMLSyntaxReader
that inherits from SyntaxReader
and adds functionality like readAttribute()
and readNode()
(the latter being recursive for children in the tree...).
I have basically just taken the working code from my lab assignment and pasted it into the appropriate places in this approach. However, when I try to read the first child, I'm getting an IOException: Pushback buffer overflow
that I can't figure out.
This is the beginning of my XML file:
<Biosfar namn="Liv">ar allt som fortplantar sig
<Rike namn="Vaxter"> etc...
I have debugged and watched the syntax readers successfully read all the way through <R
at the beginning of the child tag. At that point, the XMLSyntaxReader
confirms that it is a child tag, unreads the two last characters (R
and <
, in that order) and calls on readNode()
to parse the child node. The exception occurs when I try to unread <
.
In the debugger, I have also verified that the private pos
variable of the PushbackReader
is really set to 0
, but I can't figure out why. I've parsed a full line of text - why don't those characters still count?
I'd post my code, but it's quite lengthy and as the same worked before I'm not sure my exact implementation is relevant. If it is, please point to someplace I can upload snippets, and I'll share all the code.
发布评论
评论(2)
不是 PushbackReader 的默认大小缓冲区只有1?当您按下第二个字符时,这会导致此问题。如果增加缓冲区的大小会发生什么?
Isn't the default size of the PushbackReader buffer only 1? That would cause this problem when you push the second character. What happens if you increase the size of the buffer?
我得到了这个:
I got with it this: