简体   繁体   English

SAX解析器忽略父元素? 结果将相同命名的元素分组

[英]SAX Parser ignoring parent elements? Same named elements grouped as a result

I'm not if this is just how SAX Parser is supposed to work or if I am missing something. 如果这只是SAX Parser应该工作的方式,或者我缺少某些东西,我不是。

This is the sort of XML I am pulling down from my server: 这是我从服务器中提取的XML:

<Data>
    <Item id="1">
        <Group>
          <Name>Question One</Name>
          <type>true</type>

          <Selection>
              <Name>Answer 1</Name>
          </Selection>
        </Group>
    </Item>
    <Item id="2">
        <Group>
          <Name>Question Two</Name>
          <type>true</type>

          <Selection>
              <Name>Answer 2</Name>
          </Selection>
        </Group>
    </Item>
</Data>

As you can see, there is an element called "Name" that is showing up twice under different parents, containing different data. 如您所见,有一个名为“名称”的元素在包含不同数据的不同父项下显示两次。 I am trying to collect "Question One" and "Question Two" in an ArrayList where I have a getter and setter of setName getName. 我试图在ArrayList中收集“问题一”和“问题二”,其中我有一个setName getName的获取器和设置器。 As the title suggest my result means even the Answers are being collected into the ArrayList for that type. 如标题所示,我的结果意味着即使答案也被收集到该类型的ArrayList中。

Is there a way to only pull "Name" that refers to Question One and Question Two? 有没有办法只提取涉及问题一和问题二的“名称”? Here is how I have it now in my end element: 这是我现在在结束元素中的方式:

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    super.endElement(uri, localName, qName);

    if (qName.equalsIgnoreCase("name")) {
        dataItem.setName(currentValue);
    }

Obviously I'm only looking for name, but when I've tried it for "group" then put a second if state inside I have not had any luck either. 显然,我只是在寻找名称,但是当我尝试将其用于“组”时,如果状态里面没有任何运气,请再说一遍。 It's essentially ignoring the parents, group and selection and just collecting everything called name. 它本质上是在忽略父母,群体和选择,而只是收集所有叫做名字的东西。 Using localName also does not help. 使用localName也无济于事。

I would really appreciate your help, thanks in advance. 在此先感谢您的帮助。

SAX pays no attention to the hierarchy, but you can compensate for it in your code. SAX并不关注层次结构,但是您可以在代码中对其进行补偿。

The startElement , characters and endElement methods are called appropriately on each start tag, character content and end tag in the order that they appear, so what you want to do is recognize the <Selection> tag in your startElement and set a flag in your handler to remember to ignore any <name> tags that occur until you unset the flag on hitting the </Selection> tag and recognizing it in your endElement method. 在每个开始标记,字符内容和结束标记上,按它们出现的顺序分别调用startElementcharactersendElement方法,因此您要做的是识别startElement<Selection>标记并在处理程序中设置一个标志记住要忽略发生的任何<name>标记,直到您取消设置</Selection>标记并在endElement方法中识别出该标记为止。

If you post a bit more of your ContentHandler code, I could probably suggest specific changes. 如果您发布更多的ContentHandler代码,我可能会建议进行特定的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM