I'm in need of some help regarding a Web Service that has to retrieve and check a valid element from a XML file. The web service method should look like this - translate ("flower","english","russian") and the client should be able to allow you to enter the desired word, the original Language and the target Language. If these exist in the XML file the translation of it will be displayed on the client side, if not, it will display an error message.
I've create a standard web service method - add(int a, int b) and created also the client part using a normal java application, added a swing GUI, connected with the wsdl link and the method works nicely. How would it be best for me to make this program work? Please advice.
The service looks like this:
@WebMethod(operationName = "translate")
public String translate(@WebParam(name = "word") String word,
@WebParam(name = "originalLanguage") String originalLanguage,
@WebParam(name = "targetLanguage") String targetLanguage)
throws Exception {
XMLSource dataLSource = new XMLSource();
return dataLSource.getTranslation(word);
}
Considering that the XML file should look like this: translation.xml
<?xml version="1.0" encoding="UTF-8"?>
<translation>
<word>
<english>car</english>
<russian>avtomobil</russian>
</word>
<word>
<english>flower</english>
<russian>tsvetok</russian>
</word>
<word>
<english>butterfly</english>
<russian>babochka</russian>
</word>
</translation>
Also the XML source class:
public class XMLSource {
private Document readData() throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File xmlFile = new File("translation.xml");
return db.parse(new FileInputStream(xmlFile));
}
public String getTranslation(String original) throws Exception{
Document doc = readData();
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
XPathExpression xpe = xp.compile("/translation/word/russian[../english"
+ "/text()='"+original+"'] ");
String translated = (String) xpe.evaluate(doc, XPathConstants.STRING);
return translated;
}
}
The client GUI sample of the program