0

I need to create WEB service that will translate some words between two languages so I have created an interface:

 @WebService
public interface Translator {
    @WebMethod
    String translate(String word, String originalLanguage, String targetLanguage);
}

And class that implements that interface:

@WebService(endpointInterface = "source.Translator")
public class TranslatorImpl implements Translator{

    @Override
    public String translate(String word, String originalLanguage, String targetLanguage) {


        return word + originalLanguage +" butterfly " + targetLanguage + " baboska ";
    }

}

But because I'm very new to this I don't know how to set this webMethod to read from an XML file that is supposed to be a database with words. Right now how I did it, when I test it, it only returns the same word whatever you write. So can anybody explain to me how to read from an XML file so if I write butterfly it translate that or if I write flower it translate that. Do I do parsing of XML file in this webMethod?

1 Answer 1

0

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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

it seem that I need to do unmarshalling of the xml file to read from it and than create web service but I could really use some more help since I did only simple application's. For example if I have one xml file I know how to make xsd and do unmarshalling in Main class with output in console but I don't know how to put it in webMethod to create web service.
I added a small example how to generally achieve the retrieving of data in your webservice class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.