简体   繁体   English

无法使用sax解析器解析标签

[英]not able to parse tags using sax parser

Following is my xml: 以下是我的xml:

 <Body>
  <tag1 xmlns=""> <innerTag></innerTag> </tag1>
  </Body>

The problem is that I am not able to get the string inside <tag1></tag1> , that is <innerTag></innerTag> . 问题是我无法在<tag1></tag1>获取字符串,即<innerTag></innerTag> Following is my logic: 以下是我的逻辑:

public void startElement(final String uri, final String localName,
            final String qName, final Attributes attributes)
            throws SAXException {
        if ("tag1".equalsIgnoreCase(qName)){
            inTag1 = true;
            System.out.println("start");
        }
}


public void endElement(final String uri, final String localName,
            final String qName) throws SAXException {
        if ("tag1".equalsIgnoreCase(qName)) {
            System.out.println("end");
            inTag1 = false;
        }
}

public void characters(final char[] ch, final int start, final int length) {

        if (inTag1) {
            System.out.println("@@@" + new String(ch, start, length));
        }
    }
}

But it is giving me empty output. 但这给了我空的输出。 Can anyone help. 谁能帮忙。

In your comment to UVM's answer you wrote 在您对UVM答案的评论中,您写了

Actually inner tag is a kind of xml inside this xml. 实际上,内部标记是该xml中的一种xml。 So I want that as a whole 所以我希望整体上

There is no way to tell the SAX parser to not process all of the XML inside an element and return the entire contents as a string. 没有办法告诉SAX解析器不要处理元素中的所有XML并以字符串形式返回整个内容。 Essentially, you have two options: 本质上,您有两个选择:

  • reconstruct the XML string, by listening out to all of the SAX events and building the XML string up yourself, or 通过侦听所有SAX事件并自己构建XML字符串来重建XML字符串,或者
  • if you're in control of the XML documents you're attempting to parse, changing the format of them to something like 如果您要控制XML文档,请尝试将其格式更改为类似

     <Body> <tag1 xmlns=""><![CDATA[ <innerTag></innerTag> ]]></tag1> </Body> 

You need to check "innerTag" intead of "tag1" 您需要检查"tag1" "innerTag"

if ("innerTag".equalsIgnoreCase(qName)){
            inTag1 = true;
            System.out.println("start");
        }

Basically your innerTag is a child element of tag1.So SAX parser keep on parsing because for it, it is till a valid XML element. 基本上,您的innerTag是tag1的子元素。因此,SAX解析器会继续对其进行解析,因为对于它而言,直到有效的XML元素为止。

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

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