简体   繁体   English

如何在 Web 服务中读取 XML 文件

[英]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:我需要创建可以在两种语言之间翻译一些单词的 WEB 服务,所以我创建了一个界面:

 @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.但是因为我对此很陌生,所以我不知道如何设置这个 webMethod 来从一个 XML 文件中读取,该文件应该是一个带有单词的数据库。 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.所以任何人都可以向我解释如何从 XML 文件中读取,所以如果我写蝴蝶它会翻译它,或者如果我写花它会翻译它。 Do I do parsing of XML file in this webMethod?我是否在此 webMethod 中解析 XML 文件?

I think your question "Do I do parsing of XML file in this webMethod?"我想你的问题“我是否在这个 webMethod 中解析 XML 文件?” 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.遵循“单一职责”原则,您应该在另一个类中进行 XML 处理。

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 .关于读取 xml 文件,这里有很多问题都有很好的答案,例如Java 读取 xml 文件

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).在添加新翻译时,它比 XML 文件更灵活,并且在处理可能会更改的数据时被视为最佳实践(未来添加了许多新条目)。

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:首先,您需要类似 XmlDataSource 类的东西:

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:示例中的 xpath 依赖于这样的结构:

<?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:然后你可以调用 webservice 中的数据源来翻译请求的单词:

@Override
    public String translate(String word, String originalLanguage, String targetLanguage) {
        XmlDataSource dataSource = new XmlDataSource();
        return dataSource.getTranslation(word);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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