Erinevus lehekülje "ITI0011:praktikum 7 N14" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Uus lehekülg: 'Tagasi ITI0011 lehele. == Üldine == Praktikum: 16. oktoober kell 14.00 Praktikumis käsitletavad teemad: * käsurea argumendid * välise teegi (jar) kasutamine * tulemust...')
 
16. rida: 16. rida:
  
 
<source lang="java">
 
<source lang="java">
 +
import java.io.IOException;
 +
import java.io.StringReader;
 +
import java.text.ParseException;
 +
import java.text.SimpleDateFormat;
 +
import java.util.Calendar;
 +
import java.util.Date;
 +
 +
import javax.xml.parsers.DocumentBuilder;
 +
import javax.xml.parsers.DocumentBuilderFactory;
 +
import javax.xml.parsers.ParserConfigurationException;
 +
 +
import org.w3c.dom.Document;
 +
import org.w3c.dom.Node;
 +
import org.w3c.dom.NodeList;
 +
import org.xml.sax.InputSource;
 +
import org.xml.sax.SAXException;
 +
 +
import wiki.WikiAPI;
 +
 +
 +
public class Wiki {
 +
 +
public static void main(String[] args) {
 +
args = new String[]{"-c", "20"};
 +
// program -c 20
 +
System.out.println(args.length);
 +
for (String s : args) { // foreach
 +
System.out.println(s);
 +
/*if (s.equals("-c")) { // s == "-c"
 +
 +
}*/
 +
}
 +
 +
int count;
 +
for (int i = 0; i < args.length; i++) {
 +
String s = args[i];
 +
 +
if (s.equalsIgnoreCase("-c")) {
 +
count = Integer.parseInt(args[i + 1]);
 +
}
 +
}
 +
 +
 +
//String xml = WikiAPI.getWikiInformation("tallinn");
 +
String xml = getXml();
 +
System.out.println(xml);
 +
 +
try {
 +
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 +
DocumentBuilder db = dbf.newDocumentBuilder();
 +
StringReader sr = new StringReader(xml);
 +
InputSource is = new InputSource(sr);
 +
Document doc = db.parse(is);
 +
 +
NodeList students = doc.getElementsByTagName("student");
 +
for (int i = 0; i < students.getLength(); i++) {
 +
Node student = students.item(i);
 +
System.out.println(student.getAttributes().getNamedItem("faculty").getNodeValue());
 +
System.out.println(student.getChildNodes().item(2).getFirstChild().getNodeValue());
 +
}
 +
 +
} catch (ParserConfigurationException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
} catch (SAXException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
} catch (IOException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
}
 +
 +
 +
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
 +
String dateString = "01.01.2014";
 +
Date d = new Date();
 +
System.out.println(d);
 +
try {
 +
Date d2 = sdf.parse(dateString);
 +
 +
System.out.println(d2);
 +
Calendar c = Calendar.getInstance();
 +
c.setTime(d2);
 +
c.add(Calendar.MINUTE, 44);
 +
Date d3 = c.getTime();
 +
System.out.println(d3);
 +
} catch (ParseException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
}
 +
 +
}
 +
 +
public static String getXml() {
 +
return "<root><students><student faculty=\"I\"><name>Juhan</name><age>22</age><grade>6</grade></student><student faculty=\"M\"><name>Mari</name><age>19</age><grade>5</grade></student></students></root>";
 +
}
 +
 +
}
  
 
</source>
 
</source>

Redaktsioon: 16. oktoober 2014, kell 11:41

Tagasi ITI0011 lehele.

Üldine

Praktikum: 16. oktoober kell 14.00

Praktikumis käsitletavad teemad:

  • käsurea argumendid
  • välise teegi (jar) kasutamine
  • tulemuste parsimine


Kood tuleb üles laadida git'i. Kausta (projekti) nimi peaks olema "prax7".

Koodinäide

<source lang="java"> import java.io.IOException; import java.io.StringReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;

import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException;

import wiki.WikiAPI;


public class Wiki {

public static void main(String[] args) { args = new String[]{"-c", "20"}; // program -c 20 System.out.println(args.length); for (String s : args) { // foreach System.out.println(s); /*if (s.equals("-c")) { // s == "-c"

}*/ }

int count; for (int i = 0; i < args.length; i++) { String s = args[i];

if (s.equalsIgnoreCase("-c")) { count = Integer.parseInt(args[i + 1]); } }


//String xml = WikiAPI.getWikiInformation("tallinn"); String xml = getXml(); System.out.println(xml);

try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); StringReader sr = new StringReader(xml); InputSource is = new InputSource(sr); Document doc = db.parse(is);

NodeList students = doc.getElementsByTagName("student"); for (int i = 0; i < students.getLength(); i++) { Node student = students.item(i); System.out.println(student.getAttributes().getNamedItem("faculty").getNodeValue()); System.out.println(student.getChildNodes().item(2).getFirstChild().getNodeValue()); }

} catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }


SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); String dateString = "01.01.2014"; Date d = new Date(); System.out.println(d); try { Date d2 = sdf.parse(dateString);

System.out.println(d2); Calendar c = Calendar.getInstance(); c.setTime(d2); c.add(Calendar.MINUTE, 44); Date d3 = c.getTime(); System.out.println(d3); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }

}

public static String getXml() { return "<root><students><student faculty=\"I\"><name>Juhan</name><age>22</age><grade>6</grade></student><student faculty=\"M\"><name>Mari</name><age>19</age><grade>5</grade></student></students></root>"; }

}

</source>

Exercise

Write a program that reads argumends from command line. Every argument is a name. For every name, you should make a query to Wikipedia and retrieve some basic information.

For example, if you run the program with arguments "Tallinn" "Estonia" your program should call wikipedia two times: first with "Tallinn", then with "Estonia".

For getting information from Wikipedia, you can use http://dijkstra.cs.ttu.ee/~ago/iti0011/WikiAPI.jar library (a very simple library to just demonstrate how to use a library). This has a method getWikiInformation(String name) or getWikiInformation(String name, int format). Read javadoc for more. getWikiInformation just reads all the information from Wikiedpia API and returns it as one (huge) String.

Depending on the format you use, you should the the value of "touched" property (when the page is last accessed). If you want to use JSON-format, you can use this library: http://dijkstra.cs.ttu.ee/~ago/iti0011/json-20140107.jar

You should read out "touched" property value and represent it as a date. Try to add 44 days to that date and see, which date it is.

For date parsing, use SimpleDateFormat. To add days, use Calendar object.