Unable to start Xpath requests from JAVA in XML files with <DOCTYPE> tag

I made the following method that runs hard-coded xPath requests in a hard-coded XML file. The method works fine with one exception. Some xml files contain the following tag

           <!DOCTYPE WorkFlowDefinition SYSTEM "wfdef4.dtd"> 

When I try to run a query in this file, I get the following exception:

     java.io.FileNotFoundException: 
     C:\ProgramFiles\code\other\xPath\wfdef4.dtd(The system cannot find the file specified). 

Question: What can I do to instruct my program not to take this DTD file into account? I also noted that the path C: \ ProgramFiles \ code \ other \ xPath \ wfdef4.dtd is the one from which I run my application, and not the one where the actual XML file is located.

Thanks at advace

Here is my method:

 public String evaluate(String expression,File file){
  XPathFactory factory = XPathFactory.newInstance();
  xPath = XPathFactory.newInstance().newXPath();
  StringBuffer strBuffer = new StringBuffer();
  try{
    InputSource inputSource = new InputSource(new FileInputStream(file));
                         //evaluates the expression
    NodeList nodeList = (NodeList)xPath.evaluate(expression, 
                   inputSource,XPathConstants.NODESET);

                         //does other stuff, irrelevant with my question.
    for (int i = 0 ; i <nodeList.getLength(); i++){
     strBuffer.append(nodeList.item(i).getTextContent());
    }
  }catch (Exception e) {
   e.printStackTrace();
  }
  return strBuffer.toString();
      }
+3
source share
1 answer

And the answer is:

    xPath = XPathFactory.newInstance().newXPath();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    //add this line to ignore dth DTD
    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+1

Source: https://habr.com/ru/post/1787715/


All Articles