无法在包含<;的XML文件中从JAVA运行Xpath查询;DOCTYPE>;标签

无法在包含<;的XML文件中从JAVA运行Xpath查询;DOCTYPE>;标签,java,xml,dtd,xml-parsing,Java,Xml,Dtd,Xml Parsing,我使用以下方法在硬编码XML文件中运行硬编码xPath查询。除了一个例外,这个方法工作得非常完美。某些xml文件包含以下标记 <!DOCTYPE WorkFlowDefinition SYSTEM "wfdef4.dtd"> 问题是:我可以做什么来指示我的程序不考虑此DTD文件? 我还注意到,路径C:\ProgramFiles\code\other\xPath\wfdef4.dtd是我运行应用程序的路径,而不是实际xml文件所在的路径 谢谢你 以下是我的方法

我使用以下方法在硬编码XML文件中运行硬编码xPath查询。除了一个例外,这个方法工作得非常完美。某些xml文件包含以下标记

           <!DOCTYPE WorkFlowDefinition SYSTEM "wfdef4.dtd"> 
问题是:我可以做什么来指示我的程序不考虑此DTD文件? 我还注意到,路径C:\ProgramFiles\code\other\xPath\wfdef4.dtd是我运行应用程序的路径,而不是实际xml文件所在的路径

谢谢你

以下是我的方法:

 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();
      }
公共字符串求值(字符串表达式,文件){
XPathFactory=XPathFactory.newInstance();
xPath=XPathFactory.newInstance().newXPath();
StringBuffer strBuffer=新的StringBuffer();
试一试{
InputSource InputSource=新的InputSource(新文件InputStream(文件));
//计算表达式的值
NodeList NodeList=(NodeList)xPath.evaluate(表达式,
inputSource,XPathConstants.NODESET);
//做其他事情,和我的问题无关。
对于(int i=0;i),答案是:

    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);

这是一个类似的案例,您可能会发现它很有用:正如@adrianboimvaser所建议的,这与XPath无关,但与XML解析器无关。概念上是@aAlejandro的副本。在我的例子中,我不使用任何documentBuilderFactory,但即使使用了,我也看不到使用XPath.evaluate with的方法it@abrianboimvaser谢谢你的回复,就像我一样我不知道如何同时使用DocumentBuilderFactory和Xpath。此外,如果您引用了override方法,您必须知道您使用的是哪种DTD。在我的情况下,DTD是不同的(我没有提到),请参考:
    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);