簡體   English   中英

XMLReader相對於主應用程序搜索DTD文件而不是XML文件

[英]XMLReader searches for DTD files relatively to main app instead of XML file

我正在嘗試使用Netbeans作為IDE運行一些XML解析。 我的項目在F:\\Project ,我的XML在D:\\XML\\data.xml以及D:\\XML\\validation.dtd dtd文件在我的XML中的引用如下:

<!DOCTYPE softwarelist SYSTEM "validation.dtd">

但是我不明白為什么XMLReader相對於項目文件夾而不是XML文件夾搜索dtd?

這是解析代碼:

    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(false);
        XMLReader xmlReader = spf.newSAXParser().getXMLReader();

        xmlReader.parse(new InputSource(Files.newInputStream(path)));
    } catch (ParserConfigurationException | SAXException | IOException ex) {
        Logger.getLogger(SoftwareListLoader.class.getName()).log(Level.SEVERE, null, ex);
    }

我收到此錯誤:

java.io.FileNotFoundException:F:\\ Project \\ softwarelist.dtd(找不到指定的文件)

有沒有辦法告訴解析器相對於XML文檔文件查找dtd?

感謝Google和O'Reilly,現在我了解發生了什么。

關鍵是我將XML文件作為流傳遞給解析器,這實際上會松散對原始文件路徑的所有引用。 要解決此問題,有兩種解決方案:

1 /將原始文件路徑設置為XML文檔的系統ID。

InputSource source = new InputSource(Files.newInputStream(path));
source.setSystemId(path.toString());
xmlReader.parse(source);

2 /不要穿過溪流。

xmlReader.parse(new InputSource(path.toString()));

參考: http : //docstore.mik.ua/orelly/xml/jxml/ch03_02.htm

暫無
暫無

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

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