I think your question "Do I do parsing of XML file in this webMethod?" has not much to do with webservices in particular but with softwaredesign and -architecture. Following the "single responsibility" principle you should have the XML-handling in another class.
Regarding the reading of the xml-file there are a lot of questions with good answers here on SO as for example Java - read xml file.
By the way: Have you thought of using a database? It is more flexible when it comes to adding new translations than a XML-file and regarded as best practice when handling data that's likely to be changed (a lot of new entries added in the future).
EDIT
A little quick and dirty example to have a better understanding about what I suggested. Mind that the datastructure does not cover the usage of various languages! If you need that you have to alter the example.
First of all you need something like a XmlDataSource class:
public class XmlDataSource {
public String getTranslation(String original) throws Exception {
Document d = readData();
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/dictionary/entry/translation[../original/text() = '" + original + "']");
String translated = (String) expr.evaluate(d, XPathConstants.STRING);
return translated;
}
private Document readData() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
File datafile = new File("your/path/to/translations.xml");
return documentBuilder.parse(new FileInputStream(datafile));
}
}
The xpath in the example relies on a structure like this:
<?xml version="1.0" encoding="UTF-8"?>
<dictionary>
<entry>
<original>butterfly</original>
<translation>Schmetterling</translation>
</entry>
<entry>
<original>flower</original>
<translation>Blume</translation>
</entry>
<entry>
<original>tree</original>
<translation>Baum</translation>
</entry>
</dictionary>
Then you can call the datasource in your webservice to translate the requested word:
@Override
public String translate(String word, String originalLanguage, String targetLanguage) {
XmlDataSource dataSource = new XmlDataSource();
return dataSource.getTranslation(word);
}