我的Web服务项目中的相对路径不起作用

时间:2019-07-10 22:20:39

标签: java file web-services soap relative-path

我正在创建用于在两种语言之间翻译单词的Web服务。单词在位于项目文件夹根目录的xml文档(dictionary.xml)中定义。服务可以在绝对路径下正常运行,但是在使用相对路径进行测试时,会出现FileNotFound异常。

我尝试只写(“字典”),并且在我正在解析xml文件的项目中起作用。

这是方法:

public String translate(String word, String lang1, String lang2) throws XPathExpressionException, FileNotFoundException {
            XPathFactory factory = XPathFactory.newInstance();
            XPath path = factory.newXPath();
            String expression = "";
            if (lang1.equals("english")) {
                expression = "dictionary/word/english";
            } else if (lang1.equals("spanish")) {
                expression = "dictionary/word/spanish";
            }
            XPathExpression xPathExpression = path.compile(expression);

            File xmlDocument = new File("D:\\JavaLearning\\AssignmentWebService\\Translator\\dictionary.xml");
            InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));

            Object result = xPathExpression.evaluate(inputSource, XPathConstants.NODESET);

            NodeList nodeList = (NodeList) result;
            String translatedWord = "";
            for (int i = 0; i < nodeList.getLength(); i++) {
                if (lang1.equals("english") && word.equals(nodeList.item(i).getTextContent())) {
                    translatedWord = nodeList.item(i).getNextSibling().getNextSibling().getTextContent();
                } else if (lang1.equals("spanish") && word.equals(nodeList.item(i).getTextContent())) {
                    translatedWord = nodeList.item(i).getPreviousSibling().getPreviousSibling().getTextContent();
                }
            }
            if (translatedWord.equals("")) {
                translatedWord = "Word \"" + word + "\" doesn't exist in our database!";
            }

       return translatedWord;
    }
  

错误:由以下原因引起:service.FileNotFoundException_Exception:   dictionary.xml(系统找不到指定的文件),位于

0 个答案:

没有答案