简体   繁体   中英

How to read XML file in Web service

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM