0
  1. 単語で XML ベースを作成しますか?
  2. クライアントが翻訳で特定の単語を必要とする場合、サービスはその単語を XML ベースでチェックします。単語がある場合、サービスはクライアントへの出力として翻訳された単語をブロードキャストします。その単語が XML ファイルに存在しない場合、サービスは適切なメッセージをブロードキャストします。XML ファイルでテストするため、いくつかの単語を追加する必要があります。
  3. クライアントの利点は、translate メソッドを呼び出すことによってサービスを説明し、3 つの文字列パラメーターです。例:translate(”butterfly”, ”english”, ”russian”);

SAXパーサー:

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

ハンドラ:

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

1 に答える 1

0
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;
}

}

于 2015-07-28T07:09:32.467 に答える