Create translation web services in java?

泪湿孤枕 提交于 2019-12-08 10:38:06

问题


  1. Create XML base with words?
  2. When a client requires a particular word in the translation, the service checks for the word in its XML-based If there is a word, service as an output to the client broadcast a translated word. If the word does not exist in the XML file, the service broadcasts adequate message. Because testing in an XML file you need to add a few words.
  3. Client benefits described service by calling the method translate, three string parameters. Example: translate(”butterfly”, ”english”, ”russian”);

SAXParser:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    sp.parse ("words.xml", new MySaxHandler());
}
}

Handler:

class MySaxHandler extends DefaultHandler {
private String actualNodeName;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    this.actualNodeName = qName;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    if(!actualNodeName.equals("bs"))
        return;
    String nodeValue = new String(ch,start,length);

    if (!nodeValue.trim().equals(""))
        System.out.println (nodeValue);
}
}

回答1:


package servis;


Document doc;

public Prevodilac() throws ParserConfigurationException, SAXException, IOException{
    String xmlFile ;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xmlFile));
    doc = builder.parse(is);

}

/**
 * Web service operation
 */
@WebMethod(operationName = "pretraga")
public String pretraga(String key) {
    Element r = doc.getDocumentElement();
    NodeList language = r.getElementsByTagName("phrase");
    String result = "Not Found";
    for(int index = 0;index<language.getLength();index++){
        Node attr = language.item(index).getAttributes().getNamedItem("key");
        if(attr)
    }
    return result;
}

}



来源:https://stackoverflow.com/questions/31635608/create-translation-web-services-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!