2011-02-28 54 views
1

我需要解析一大堆傳入的XML文檔,但它不包含DOCTYPE(它們都具有不同的DTD)。 DTD是由我自己創建的。我如何根據本地存儲爲文件的DTD驗證XML文件?我有以下要求:如何使用java驗證XML?

  1. 所有DTD(對於不同的XML)當傳入的XML到達時不會查看本地存儲的區域,將一次加載到內存中。
  2. 根據加載DTD文件驗證傳入的XML。

感謝

+2

這似乎重複的問題「驗證對一個XML文件本地DTD文件與Java「 – 2011-02-28 10:28:32

回答

1

您需要使用本地實體解析器上你的SAX解析器,這裏是如何執行的一個例子:

class LocalEntityResolver implements EntityResolver { 

private static final Logger LOG = ESAPI.getLogger(LocalEntityResolver.class); 
private static final Map<String, String> DTDS; 
static { 
    DTDS = new HashMap<String, String>(); 
    DTDS.put("-//W3C//DTD XHTML 1.0 Transitional//EN", 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"); 
    DTDS.put("-//W3C//ENTITIES Latin 1 for XHTML//EN", 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent"); 
    DTDS.put("-//W3C//ENTITIES Symbols for XHTML//EN", 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent"); 
    DTDS.put("-//W3C//ENTITIES Special for XHTML//EN", 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent"); 
} 

@Override 
public InputSource resolveEntity(String publicId, String systemId) 
     throws SAXException, IOException { 
    InputSource input_source = null; 
    if (publicId != null && DTDS.containsKey(publicId)) { 
     LOG.debug(Logger.EVENT_SUCCESS, "Looking for local copy of [" + publicId + "]"); 

     final String dtd_system_id = DTDS.get(publicId); 
     final String file_name = dtd_system_id.substring(
       dtd_system_id.lastIndexOf('/') + 1, dtd_system_id.length()); 

     InputStream input_stream = FileUtil.readStreamFromClasspath(
       file_name, "your/dtd/location", 
       getClass().getClassLoader()); 
     if (input_stream != null) { 
      LOG.debug(Logger.EVENT_SUCCESS, "Found local file [" + file_name + "]!"); 
      input_source = new InputSource(input_stream); 
     } 
    } 

    return input_source; 
} 
} 
+0

哦,然後你會這樣使用它 DocumentBuilder builder = DocumentBuilderFactory .newInstance()。newDocumentBuilder(); builder.setEntityResolver(new LocalEntityResolver()); document = builder.parse(is); – epoch 2011-02-28 10:58:06