[英]Issue in XML parsing using SAX
I have a XML which i am trying to parse. 我有一个XML,我正在尝试解析。
<Tests>
<Test>
<Blocks>
<Block>
<BlockId>2</BlockId>
<Name>CCCC</Name>
<Type>Action</Type>
<TaskId>2</TaskId>
<Send>
<WId>284</WId>
<BlockId>14</BlockId>
</Send>
</Block>
<Block>
<BlockId>10</BlockId>
<Name>START VM4</Name>
<Type>Action</Type>
<TaskId>10</TaskId>
<Send />
</Block>
<Block>
<BlockId>12</BlockId>
<Name>SHUT</Name>
<Type>Action</Type>
<TaskId>12</TaskId>
<Send />
</Block>
</Blocks>
</Tests>
</Test>
I am using SAX to parse this. 我正在使用SAX对此进行解析。 Everything works fine, but every time i loop through, i should get a block with id 2 and then another block with blockid 10 and then 12. and i am then adding to all these blocks to the test.
一切正常,但每次循环浏览时,我都应获取一个ID为2的块,然后再获取另一个ID为10的块,然后再获取12。然后,我将所有这些块添加到测试中。
Portion of my code is: 我的代码部分是:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
nqName = qName;
tag_name_List.setElementAt(nqName, level);
level = level + 1;
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
level = level - 1;
tag_name_List.removeElementAt(level);
}
public void characters(char ch[], int start, int length) throws SAXException {
if (level != 0) {
////////////////Some code
} else if (level == 5
&& tag_name_List.elementAt(1).equals("Test")
&& tag_name_List.elementAt(2).equals("Blocks")
&& tag_name_List.elementAt(3).equals("Block")
&& (nqName.equalsIgnoreCase("BlockId"))) {
block = new Block();
test.addBlock(block);
block.setId(new String(ch, start, length));
block.setWorkflowId(workflow.getId());
} else if (level == 5 && ...) {
////// Code continues
NB This is a huge xml and huge code, so just sharing partly... 注意:这是一个巨大的xml和巨大的代码,因此仅部分共享...
But the issue here is: 但是这里的问题是:
id
as 2
, id
为2
, "\\n "
"\\n "
id
as 10
id
为10
"\\n "
"\\n "
id
12
id
12
"\\n "
. "\\n "
。 I am not sure why i am getting these "\\n "
. 我不确定为什么要得到这些
"\\n "
。
I can put a if
condition to avoid that entity, but if i do so i lose some information attached to that id
, which later gets associated with that "\\n "
id
. 我可以设置一个
if
条件来避免该实体,但是如果这样做,我会丢失一些附加到该id
,该信息后来与该"\\n "
id
关联。
Has anyone faced this and can give a pointer. 有没有人面对过这个问题,可以给出一个指示。
Let me know if more information is needed. 让我知道是否需要更多信息。
After debugging the code i found that, it is basically taking the "\\n "
from the end of 调试代码后,我发现它基本上是从结尾处取
"\\n "
<BlockId>14</BlockId>
Since there will be a \\r
and "\\n "
for the next line here. 因为这里的下一行将有一个
\\r
和"\\n "
。
How can i avoid this? 我如何避免这种情况?
You assign nqName = qName
. 您分配
nqName = qName
。 Do you ever change that value until the next iteration? 您是否更改过该值直到下一次迭代?
If you don't change that value when you leave the context of the BlockId
element, it will still be equal to BlockId
when you are outside the element but not yet inside Name
, for example. 例如,如果在离开
BlockId
元素的上下文时不更改该值,则当您位于元素外部但尚未位于Name
内部时,该值仍等于 BlockId
。 And the characters()
method will read all the whitespace it finds there. 而且
characters()
方法将读取在那里找到的所有空白。
There should probably reset nqName
in your endElement()
. 可能应该在
endElement()
重置nqName
。 Try adding 尝试添加
nqName = null;
to your endElement()
method. 到您的
endElement()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.