簡體   English   中英

如何使用Java中的“打開和結束標記不匹配”來解析XML文件

[英]How to parse XML file with “Opening and ending tag mismatch” in Java

我有和打開Price標簽的XML文件。 盡管有錯誤,有沒有辦法解析文件? 如何跳過錯誤的產品並繼續解析?

<Products>
      <Product Name="Gummi bears">
        <Price Currency="GBP">4.07</Price>
        <BestBefore Date="19-02-2014"/>
      </Product>
      <Product Name="Mounds">
        <Price Currency="AUD">5.64
        <BestBefore Date="08-04-2014"/>
      </Product>
      <Product Name="Vodka">
        <Price Currency="RUB">70</Price>
        <BestBefore Date="11-10-2014"/>
      </Product>
  </Products>

這是代碼。 這是BrandonArp已經提到的實現。

有一個屬性需要設置為忽略致命錯誤 - 繼續致命錯誤

http://apache.org/xml/features/continue-after-fatal-error 
true:   Attempt to continue parsing after a fatal error.  
false:  Stops parse on first fatal error.  
default:    false  
XMLUni Predefined Constant:     fgXercesContinueAfterFatalError  
note:   The behavior of the parser when this feature is set to true is undetermined! Therefore use this feature with extreme caution because the parser may get stuck in an infinite loop or worse.  

更多細節可以在這里找到

PriceReader類

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.XMLReader;

public class PriceReader {

    public static void main(String argv[]) {

        try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        XMLReader xmlReader = saxParser.getXMLReader();

        try {
            xmlReader.setFeature(
                            "http://apache.org/xml/features/continue-after-fatal-error",
                            true);
        } catch (SAXException e) {
            System.out.println("error in setting up parser feature");
        }

        xmlReader.setContentHandler(new PriceHandler());
        xmlReader.setErrorHandler(new MyErrorHandler());
        xmlReader.parse("bin\\com\\test\\stack\\overflow\\sax\\prices.xml");

    } catch (Throwable e) {
         System.out.println("Error -- " +e.getMessage());
    }

    }
}

PriceHandler類

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class PriceHandler extends DefaultHandler {

    public void startElement(String uri, String localName,
        String qName, Attributes attributes)
        throws SAXException {

    if (qName.equalsIgnoreCase("Product")) {
        System.out.println("Product ::: "+ attributes.getValue("Name"));
    }
  }
}

MyErrorHandler類

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class MyErrorHandler implements ErrorHandler {

    private String getParseExceptionInfo(SAXParseException spe) {
        String systemId = spe.getSystemId();

        if (systemId == null) {
            systemId = "null";
        }

        String info = "URI=" + systemId + " Line=" 
            + spe.getLineNumber() + ": " + spe.getMessage();

        return info;
    }

    public void warning(SAXParseException spe) throws SAXException {
        System.out.println("Warning: " + getParseExceptionInfo(spe));
    }

    public void error(SAXParseException spe) throws SAXException {
        String message = "Error: " + getParseExceptionInfo(spe);
        System.out.println(message);
    }

    public void fatalError(SAXParseException spe) throws SAXException {
        String message = "Fatal Error: " + getParseExceptionInfo(spe);
        System.out.println(message);
    }
}

產量

 Product ::: Gummi bears
Product ::: Mounds
Fatal Error: URI=file:///C:/Developer/pachat/workspaces/eclipse-default/stack-overflow/bin/com/test/stack/overflow/sax/prices.xml Line=9: The element type "Price" must be terminated by the matching end-tag "</Price>".
Fatal Error: URI=file:///C:/Developer/pachat/workspaces/eclipse-default/stack-overflow/bin/com/test/stack/overflow/sax/prices.xml Line=9: The end-tag for element type "Price" must end with a '>' delimiter.
Product ::: Vodka
Product ::: Rum
Product ::: Brezzer
Fatal Error: URI=file:///C:/Developer/pachat/workspaces/eclipse-default/stack-overflow/bin/com/test/stack/overflow/sax/prices.xml Line=21: The element type "Price" must be terminated by the matching end-tag "</Price>".
Fatal Error: URI=file:///C:/Developer/pachat/workspaces/eclipse-default/stack-overflow/bin/com/test/stack/overflow/sax/prices.xml Line=21: The end-tag for element type "Price" must end with a '>' delimiter.
Product ::: Water
Fatal Error: URI=file:///C:/Developer/pachat/workspaces/eclipse-default/stack-overflow/bin/com/test/stack/overflow/sax/prices.xml Line=26: The end-tag for element type "Product" must end with a '>' delimiter.
Fatal Error: URI=file:///C:/Developer/pachat/workspaces/eclipse-default/stack-overflow/bin/com/test/stack/overflow/sax/prices.xml Line=26: XML document structures must start and end within the same entity.
Fatal Error: URI=file:///C:/Developer/pachat/workspaces/eclipse-default/stack-overflow/bin/com/test/stack/overflow/sax/prices.xml Line=26: Premature end of file.
 Error -- processing event: -1

處理這類錯誤的一般方法是使用流式解析器。 想到Java的是SAX。

創建Handler時,您將能夠覆蓋/實現errorfatalError方法。 這些將允許您繼續解析,但仍然會讓您處理實際的錯誤。

顯然,XML文檔中存在許多可能的錯誤,只處理其中的一些錯誤才有意義。 希望這會給你一個從解析器開始的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM