简体   繁体   中英

Get first tag Sax parser

有没有一种方法可以获取xml文件中的第一个标签,并使用sax解析器确保它具有相应的结束标签?

只需处理endDocument,如果调用它,则格式正确。

You can either handle startElement() , endElement() and endDocument() , or just handle endDocument() . endDocument() should throw an exception if the document is not well-formed. However, for the sake of learning I will show a few examples:

public class MyParser extends DefaultHandler {

    private String firstElement;
    private String lastElement;

    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
        if(firstElement == null) {
            firstElement = name;
        } 
    }
    public void endElement(String uri, String localName, String name) throws SAXException {
        lastElement = name;
    }
    public void endDocument() {
        if(lastElement.equals(firstElement)) {
            // Well formed input
        }
    }
}

You can also ensure all elements are closed with a stack:

public class MyParser extends DefaultHandler {
    Stack<String> stk;

    //...

    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
       stk.push(name);
    }  

     public void endElement(String uri, String localName, String name) throws SAXException  {
         if(stk.pop().equals(name)) {
            // Input is well formed for each tag
         }
         else {
            // Not well-formed
         }
     }
}

This sounds more like you want to use DOM parsing.

If you use sax parsing, you are actually saying you do not want to process (load in memory) the entire document at once. If you search for the first tags end (the root tag), You are scanning the entire document at once, and loose the benefit of SAX.

The DOM parses will also throw when you load the document and it is not well-formed. So no need to manually check whether the root tag was closed.

如果没有打开或关闭任何标签,则SAX解析器实际上会引发异常,因此不需要对其进行处理....因此,如果XML文件错误/损坏,则SAX解析器将引发异常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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