Task from java web services and XML - java

This is the task from java web services and XML:
Create a translation service.
Customer service to activate the service method as follows:
getWord ("automobil", "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).
This is the java doc:
package xmlparsiranje;
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 Xmlparsiranje {
public static void main(String[] argv) throws Exception {
// try {
File fXmlFile = new File("C:\\zaTestiranje.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 rijec = word.nextLine();
Scanner izvoriste = new Scanner(System.in);
System.out.println("Izvorni: ");
String izvorni = izvoriste.nextLine();
Scanner Odrediste = new Scanner(System.in);
System.out.println("Odrediste: ");
String odredisni = Odrediste.nextLine();
NodeList nList = doc.getElementsByTagName("word");
// System.out.println(odredisni);
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
Element eElement = (Element) nNode;
NodeList engleski = eElement.getElementsByTagName("english");
NodeList ruski = eElement.getElementsByTagName("russian");
NodeList poljski = eElement.getElementsByTagName("polish");
// System.out.println(engleski.item(0).getFirstChild().getTextContent());
if (odredisni.equals("english"))
{
if(izvorni.equals("russian")){
if(ruski.item(0).getFirstChild().getTextContent().equals(rijec))
{
System.out.println(ruski.item(0).getFirstChild().getTextContent());
System.out.println(engleski.item(0).getFirstChild().getTextContent());
}
}
if(izvorni.equals("polish")) {
if(poljski.item(0).getFirstChild().getTextContent().equals(rijec)) {
System.out.println(poljski.item(0).getFirstChild().getTextContent());
System.out.println(engleski.item(0).getFirstChild().getTextContent());
}
}
}
if (odredisni.equals("russian"))
{
if(izvorni.equals("english")){
if(engleski.item(0).getFirstChild().getTextContent().equals(rijec))
{
System.out.println(engleski.item(0).getFirstChild().getTextContent());
System.out.println(ruski.item(0).getFirstChild().getTextContent());
}
}
if(izvorni.equals("polish")) {
if(poljski.item(0).getFirstChild().getTextContent().equals(rijec)) {
System.out.println(poljski.item(0).getFirstChild().getTextContent());
System.out.println(ruski.item(0).getFirstChild().getTextContent());
}
}
}
if (odredisni.equals("polish"))
{
if(izvorni.equals("english")){
if(engleski.item(0).getFirstChild().getTextContent().equals(rijec))
{
System.out.println(engleski.item(0).getFirstChild().getTextContent());
System.out.println(poljski.item(0).getFirstChild().getTextContent());
}
}
if(izvorni.equals("russian")) {
if(poljski.item(0).getFirstChild().getTextContent().equals(rijec)) {
System.out.println(poljski.item(0).getFirstChild().getTextContent());
System.out.println(ruski.item(0).getFirstChild().getTextContent());
}
}
}
/* String trazenaRijec = getTagValue("english", eElement);
String engleski = getTagValue("english", eElement);
String ruski = getTagValue("russian", eElement);
String poljski = getTagValue("polish", eElement);
if (odredisni.equals(engleski))
{
System.out.println("Engleski : " + getTagValue("english", eElement));
}
if (odredisni.equals(ruski))
{
System.out.println("Ruski : " + getTagValue("russian", eElement));
}
if (odredisni.equals(poljski))
{
System.out.println("Poljski : " + getTagValue("polish", eElement));
} */
/* 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();
}
}
And 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>
<polish>Zajety</polish>
</word>
</translate>
It didnt get accepted by the instructor. He says there is no service.
What am I doing wrong?

Related

to find the attribute value of a tag when the specific text matches in the content

i want to get the value of id , if the child of the tag contains specific text.
Input :
<base>
<parent id="101" txt="hello">
<child1>
<data> search </data>
</child1>
<child2>
<data> send</data>
</child2>
</parent>
<parent id="102" txt="hello">
<child1>
<data> hai </data>
</child1>
<child2>
<data> hey </data>
</child2>
</parent>
</base>
Output:
i'm searching for "hey" text in the entire file , so it should return
id="102"
Code Snippet which i tried
if(line.indexOf("<Parent")>= 0)
{
String output="";
Pattern pat = Pattern.compile("id=\".*?\"");
Matcher mat = pat.matcher(line);
if(mat.find())
{
int start=mat.start();
int end=mat.end();
output = line.substring(start+4,end-1);
}
Pattern pat1 = Pattern.compile("<parent"[A-Z](?i)[^.?!]*?\\b(hey)\\b[^.?!]*[.?!]")</parent>");
Matcher mat1 = pat.matcher(line);
if(mat.find())
{
bw.write(output);
}
}
Try this : It should give you the text :
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class ReadXML {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
File fXmlFile = new File("Path to your xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("parent");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Parent id : " + eElement.getAttribute("id"));
System.out.println("Parent txt : " + eElement.getAttribute("txt"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
try{
bw = new BufferedWriter(new FileWriter(outfilename));
br = new BufferedReader(new FileReader(infilename));
while((line=br.readLine())!=null){
if(line.indexOf("<PGBLK")>= 0){
Pattern pat = Pattern.compile("KEY=\".*?\"");
Matcher mat = pat.matcher(line);
if(mat.find()){
int start=mat.start();
int end=mat.end();
output = line.substring(start+5,end-1);
}
}
Pattern pat1 = Pattern.compile(".*?Reference dimensions do not require inspection.*?");
Matcher mat1 = pat1.matcher(line);
if(mat1.find()){
bw.write(output);
bw.newLine();
}
}
}
Below is the code to do this in VTD-XML
import com.ximpleware.*;
public class xpathSearch {
public static void main(String s[])throws VTDException{
VTDGen vg = new VTDGen();
if (!vg.parseFile("d:\\xml\\input.txt", false))
return;
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/base/parent/*/data[contains(.,'hey')]/../../#id");
int i;
while ((i=ap.evalXPath())!=-1)
System.out.println("attr id has the value of "+vn.toString(i+1));
}
}

How to split a split a string twice

I am trying to split a string twice
String example = response;
String [] array = example.split("<section>");
System.out.println(array[0]);
String [] array2 = example.split("<title>");
System.out.println(array2[2]);
I am trying to achieve this by using this code (not successfully), but instead of printing the first split i want to save it and carry on with the 2nd split. whould anyone have a solution to ths problem or better way of going about splitting a string twice? thanks
This may seem like alot... but you should really be using a DOM parser for manipulating XML:
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
public class ExtractXML {
public static void main(String argv[]) {
DocumentBuilderFactory docBuilderFactory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
String rawStr = "Response: <section><title>Input interpretation</title>"
+ "<sectioncontents>Ireland</sectioncontents></section>"
+ "<section><title>Result</title>"
+ "<sectioncontents>Michael D. Higgins</sectioncontents></section>";
String docStr = rawStr.substring(rawStr.indexOf('<'));
String answer = "";
try {
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(new InputSource(new StringReader(docStr)));
} catch (SAXParseException e) {
System.out.println("Doc missing root node, adding and trying again...");
docStr = String.format("<root>%s</root>", docStr);
try {
doc = docBuilder.parse(new InputSource(new StringReader(docStr)));
} catch (Exception e1) {
System.out.printf("Malformed XML: %s\n", e1.getMessage());
System.exit(0);
}
} catch (Exception e) {
System.out.printf("Something went wrong: %s\n", e.getMessage());
} finally {
try {
// Normalize text representation:
doc.getDocumentElement().normalize();
NodeList titles = doc.getElementsByTagName("title");
for (int tIndex = 0; tIndex < titles.getLength(); tIndex++) {
Node node = titles.item(tIndex);
if (node.getTextContent().equals("Result")) {
Node parent = node.getParentNode();
NodeList children = parent.getChildNodes();
for (int cIndex = 0; cIndex < children.getLength(); cIndex++) {
Node child = children.item(cIndex);
if (child.getNodeName() == "sectioncontents") {
answer = child.getTextContent();
}
}
}
}
System.out.printf("Answer: %s\n", answer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Output:
[Fatal Error] :1:98: The markup in the document following the root element must be well-formed.
Doc missing root node, adding and trying again...
Answer: Michael D. Higgins
I really don't think you want use split here. I think you want to use something like
// Extract a given tag value from an input txt.
public static String extractTagValue(String txt,
String tag) {
if (tag == null || txt == null) {
return "";
}
String lcText = txt.toLowerCase();
tag = tag.trim().toLowerCase();
String openTag = "<" + tag + ">";
String closeTag = "</" + tag + ">";
int pos1 = lcText.indexOf(openTag);
if (pos1 > -1) {
pos1 += openTag.length();
int pos2 = lcText.indexOf(closeTag, pos1 + 1);
if (pos2 > -1) {
return txt.substring(pos1, pos2);
}
}
return "";
}
public static void main(String[] args) {
String example = "<title>Hello</title><section>World</SECTION>";
String section = extractTagValue(example,
"section");
String title = extractTagValue(example, "title");
System.out.printf("%s, %s\n", title, section);
}
Which, when executed, outputs
Hello, World

Parsing XML from webpage

If I copy and paste the xml from this site into a xml file I can parse it with java
http://api.indeed.com/ads/apisearch?publisher=8397709210207872&q=java&l=austin%2C+tx&sort&radius&st&jt&start&limit&fromage&filter&latlong=1&chnl&userip=1.2.3.4&v=2
However, I want to parse it directly from a webpage if possible!
Here's my current code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
public class XMLParser {
public void readXML(String parse) {
File xml = new File(parse);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
// System.out.println("Root element :"
// + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("result");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
// System.out.println("\nCurrent Element :" +
nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("job title : "
+
eElement.getElementsByTagName("jobtitle").item(0)
.getTextContent());;
System.out.println("Company: "
+
eElement.getElementsByTagName("company")
.item(0).getTextContent());
System.out.println("City : "
+
eElement.getElementsByTagName("city").item(0)
.getTextContent());
System.out.println("State : "
+
eElement.getElementsByTagName("state").item(0)
.getTextContent());
System.out.println("Country : "
+
eElement.getElementsByTagName("country").item(0)
.getTextContent());
System.out.println("Date posted : "
+
eElement.getElementsByTagName("date").item(0)
.getTextContent());
System.out.println("Job summary : "
+
eElement.getElementsByTagName("snippet").item(0)
.getTextContent());
System.out.println("Latitude : "
+
eElement.getElementsByTagName("latitude").item(0).getTextContent());
System.out.println("longitude : "
+
eElement.getElementsByTagName("longitude").item(0).getTextContent());
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new XMLParser().readXML("test.xml");
}
}
any help would be appreciated.
Give it the URI instead of the XML. It will download it for you.
Document doc = dBuilder.parse(uriString)
Please find the code snippet like this
String url = "http://api.indeed.com/ads/apisearch?publisher=8397709210207872&q=java&l=austin%2C+tx&sort&radius&st&jt&start&limit&fromage&filter&latlong=1&chnl&userip=1.2.3.4&v=2";
try
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse(url);
}
you need to have the element/nodes you want in a for loop. So it can scan through xml file, and find the right node you searching for.
reads the xml file as a string, and creates a xml structure
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(connection.getInputStream());
NodeList nodes = doc.getElementsByTagName("mode");
for (int i = 0; i < nodes.getLength(); i++)
Element element = (Element) nodes.item(i);
//Gets tag from XML and it´s content
NodeList nodeMode = element.getElementsByTagName("mode");
Element elemMode = (Element) nodeMode.item(0);
and after if you want to pick out a value and parse to an int or what you want you do like this:
int currentMode = Integer.parseInt(elemMode.getFirstChild().getTextContent());
That's how I parsed data directly from url http://www.nbp.pl/kursy/xml/+something
static class Kurs {
public float kurs_sprzedazy;
public float kurs_kupna;
}
private static DocumentBuilder dBuilder;
private static Kurs getData(String filename, String currency) throws Exception {
Document doc = dBuilder.parse("http://www.nbp.pl/kursy/xml/"+filename+".xml");
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("pozycja");
for(int i = 0; i < nList.getLength(); i++) {
Element nNode = (Element)nList.item(i);
if(nNode.getElementsByTagName("kod_waluty").item(0).getTextContent().equals(currency)) {
Kurs kurs = new Kurs();
String data = nNode.getElementsByTagName("kurs_sprzedazy").item(0).getTextContent();
data = data.replace(',', '.');
kurs.kurs_sprzedazy = Float.parseFloat(data);
data = nNode.getElementsByTagName("kurs_kupna").item(0).getTextContent();
data = data.replace(',', '.');
kurs.kurs_kupna = Float.parseFloat(data);
return kurs;
}
}
return null;
}

reading xml - java, dom

I have a problem with reading data from xml using dom. I don't know why "System.out.println(nNode.getChildNodes().item(0).hasAttributes());" returns false... In my xml file this node contains attributes. Could you help me please?
This is my code:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlParser {
private String[] linia;
private String[] wariant;
private String[] przystanek;
private String[] tabliczka;
private String[] dzien;
private String[] godz;
private String[] min;
public void readXml() {
try {
File fXmlFile = new File("c:\\file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"
+ doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("linia");
System.out.println("-----------------------");
Node nNode = nList.item(0);
linia = new String[nNode.getAttributes().getLength()];
System.out.println(nNode.getAttributes().getLength());
int i = 0;
while (i < nNode.getAttributes().getLength()) {
linia[i] = nNode.getAttributes().item(i) + "";
System.out.print(linia[i] + " ");
i++;
}
wariant = new String[nNode.getChildNodes().getLength()];
System.out.println();
System.out.println(nNode.getChildNodes().getLength());
System.out.println(nNode.getNodeName());
int j = 0;
System.out.println(nNode.getChildNodes().item(0).hasAttributes());
while (j < nNode.getChildNodes().getLength()) {
wariant[j] = nNode.getChildNodes().item(j).getAttributes()
.item(0)
+ "";
// if(wariant[j].toString()!=null)
System.out.println(" " + wariant[j]);
j++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Have you checked the child node at index 1? My guess is that your parser sees all characters between tags (newlines, tabs, spaces) as CDATA and parses them as CDATA nodes which do not have attributes.

Failing to get element values using Element.getAttribute()

I would like to read an xml file. I' ve found an example which is good until the xml element doesn't have any attributes. Of course i've tried to look after how could I read attributes, but it doesn't works.
XML for example
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<car>
<properties>
<test h="1.12" w="4.2">
<colour>red</colour>
</test>
</properties>
</car>
Java Code:
public void readXML(String file) {
try {
File fXmlFile = new File(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
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("test : "
+ getTagValue("test", eElement));
System.out.println("colour : " + getTagValue("colour", eElement));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();
Node nValue = (Node) nlList.item(0);
System.out.println(nValue.hasAttributes());
if (sTag.startsWith("test")) {
return eElement.getAttribute("w");
} else {
return nValue.getNodeValue();
}
}
Output:
false
test :
false
colour : red
My problem is, that i can't print out the attributes. How could i get the attributes?
There is alot wrong with your code; undeclared variables and a seemingly crazy algorithm. I rewrote it and it works:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public final class LearninXmlDoc
{
private static String getTagValue(final Element element)
{
System.out.println(element.getTagName() + " has attributes: " + element.hasAttributes());
if (element.getTagName().startsWith("test"))
{
return element.getAttribute("w");
}
else
{
return element.getNodeValue();
}
}
public static void main(String[] args)
{
final String fileName = "c:\\tmp\\test\\domXml.xml";
readXML(fileName);
}
private static void readXML(String fileName)
{
Document document;
DocumentBuilder documentBuilder;
DocumentBuilderFactory documentBuilderFactory;
NodeList nodeList;
File xmlInputFile;
try
{
xmlInputFile = new File(fileName);
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.parse(xmlInputFile);
nodeList = document.getElementsByTagName("*");
document.getDocumentElement().normalize();
for (int index = 0; index < nodeList.getLength(); index++)
{
Node node = nodeList.item(index);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;
System.out.println("\tcolour : " + getTagValue(element));
System.out.println("\ttest : " + getTagValue(element));
System.out.println("-----");
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}
If you have a schema for the file, or can make one, you can use XMLBeans. It makes Java beans out of the XML, as the name implies. Then you can just use getters to get the attributes.
Use dom4j library.
InputStream is = new FileInputStream(filePath);
SAXReader reader = new SAXReader();
org.dom4j.Document doc = reader.read(is);
is.close();
Element content = doc.getRootElement(); //this will return the root element in your xml file
List<Element> methodEls = content.elements("element"); // this will retun List of all Elements with name "element"
Attribute attrib = methodEls.get(0).attribute("attributeName"); // this is the "attributeName" attribute of first element with name "element"
If you're looking purely to obtain attributes (E.g. a config / ini file) I would recommend using a java properties file.
http://docs.oracle.com/javase/tutorial/essential/environment/properties.html
If you just want to read a file create a new fileReader and put it into a bufferedReader.
BufferedReader in = new BufferedReader(new FileReader("example.xml"));

Categories