5 Replies - 1432 Views - Last Post: 08 November 2012 - 06:33 AM Rate Topic: -----

#1 @passat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-November 12

How to create a simple java web service that will read from XML file?

Posted 07 November 2012 - 03:22 AM

This is the task:

Create a translation service.

Customer service to activate the service method as follows:

getWord ("car", "russian", "polish")


The first parameter is the required word, the second is the original language, and the third target language.

The method should return a string with the appropriate word or words separated by commas if there are synonyms.

Data source, the service should use XML documents (the system may have only a few words, in order to test the functionality).


I started by creating an XML file and establishing a connection between Java and XML:

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class Main {

    
    public static void main(String[] args) throws
            ParserConfigurationException, SAXException, IOException{
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        sp.parse ("Servis.xml", new MySaxHandler());
               }
}



Then I read the XML file:


import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;


public 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("english"))
            return;
        String nodeValue = new String (ch, start, length);
        
               
        if (!nodeValue.trim().equals(""))
            System.out.println (nodeValue);
                
      }
    
}






I have no idea what to do next. How can I filter (and compare) data from XML file?

Is This A Good Question/Topic? 0
  • +

Replies To: How to create a simple java web service that will read from XML file?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,655
  • Joined: 20-September 08

Re: How to create a simple java web service that will read from XML file?

Posted 07 November 2012 - 04:44 AM

Please post an example xml file
Was This Post Helpful? 0
  • +
  • -

#3 @passat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-November 12

Re: How to create a simple java web service that will read from XML file?

Posted 07 November 2012 - 05:39 AM

View Postg00se, on 07 November 2012 - 04:44 AM, said:

Please post an example xml file


This is XML file:

<?xml version="1.0" encoding="UTF-8"?>
<translate>
<word>
	<english>Car</english>
	<russian>Avtomobil</russian>
	<polish>Samochod</polish>
</word>
<word>
	<english>Love</english>
	<russian>Lobite</russian>
	<polish>milosc</polish>
</word>
<word>
	<english>Busy</english>
	<russian>Zanimate</russian>
	<russian>Zanito</russian>
	<polish>Zajety</polish>
</word>
</translate>



Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,655
  • Joined: 20-September 08

Re: How to create a simple java web service that will read from XML file?

Posted 07 November 2012 - 06:17 AM

You're going to find it easier to use DOM-wise parsing: http://docs.oracle.c...va.lang.String)
Was This Post Helpful? 0
  • +
  • -

#5 @passat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-November 12

Re: How to create a simple java web service that will read from XML file?

Posted 08 November 2012 - 06:15 AM

View Postg00se, on 07 November 2012 - 06:17 AM, said:

You're going to find it easier to use DOM-wise parsing: http://docs.oracle.c...va.lang.String)


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.util.Scanner;
public class ReadXML {
    
    public static void main(String[] argv) {
 
	  try {
    	File fXmlFile = new File("Servis.xml");
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
                Scanner word = new Scanner(System.in);
                System.out.println("Input word: ");
                String nextWord = word.next();
                
 
		NodeList nList = doc.getElementsByTagName("word");
                                
		for (int temp = 0; temp < nList.getLength(); temp++) {
 
		   Node nNode = nList.item(temp);
		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
		      Element eElement = (Element) nNode;
                      
                      } 
		      System.out.println("English : " + getTagValue("english", eElement));
		      System.out.println("Russian : " + getTagValue("russian", eElement));
                      System.out.println("Polish : " + getTagValue("polish", eElement));
	              
		      
 
		   }
		}
	  } catch (Exception e) {
		e.printStackTrace();
	  }
  }
 
  private static String getTagValue(String sTag, Element eElement) {
	NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
 
        Node nValue = (Node) nlList.item(0);
 
	return nValue.getNodeValue();
  }
 
}



In this way I can print the content of an XML file, but how can I compare the content from XML file with string "nextWord" (user input)and print only a part of values from XML file. For example, if a user inputs a word "Car" it should return only translation for word "Car".
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,655
  • Joined: 20-September 08

Re: How to create a simple java web service that will read from XML file?

Posted 08 November 2012 - 06:33 AM

I would use a Map<String, List<String>> for your nodes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1