Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Web Service & XML
    text
    copied!<p>I would need to build a simple program for my homework purposes that will retrieve data from an XML attribute based on the user input in a web service. To that end, I assumed I would start building a class that could parse my XML string and also I built a simple java service that does nothing but responds with a simple message. The problem is how do I put these together in order to get my program to work? Is this a good way to begin with? Please advise.</p> <p>Also, to make thing a little more easier, the data in the string representation of XML has key words in both English and Serbian that would enable this web service to retrieve from one another:</p> <pre><code>import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Recnik { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { String xmlString = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;!DOCTYPE language [&lt;!ATTLIST phrase id ID #IMPLIED&gt;]&gt;&lt;language id=\"sr\"&gt;&lt;phrase key=\"house\" value=\"kuca\"/&gt;&lt;phrase key=\"dog\" value=\"pas\"/&gt;&lt;phrase key=\"cat\" value=\"macka\"/&gt;&lt;/language&gt;"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); //FileInputStream fis = new FileInputStream("myBooks.xml"); InputSource is = new InputSource(new StringReader(xmlString)); Document doc = db.parse(is); Element r = doc.getDocumentElement(); NodeList language = r.getElementsByTagName("phrase"); System.out.println(language.item(1).getAttributes().item(0).getTextContent()); } } package Prevodilac; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; @WebService(serviceName = "Prevodilac") public class Prevodilac { @WebMethod(operationName = "pretraga") public String pretraga(int a, int b) { Integer res = a+b; return res.toString(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload