fork download
  1. import java.net.URL;
  2.  
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import org.w3c.dom.Document;
  6. import org.w3c.dom.Element;
  7. import org.w3c.dom.NodeList;
  8.  
  9. class ReadXMLFromServer {
  10. public static void main(String [] args){
  11. parseFile();
  12. }
  13. public static void parseFile() {
  14. //get the factory
  15. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  16. try {
  17. String url = "http://t...content-available-to-author-only...b.net/Trip/rs/TripService/v1/Trip/c1d52b4d-7342-40f2-90fe-64ee173662b2";
  18.  
  19. //Using factory get an instance of document builder
  20. DocumentBuilder db = dbf.newDocumentBuilder();
  21. //parse using builder to get DOM representation of the XML file
  22. //Document dom = db.parse("employees.xml");
  23. Document dom = db.parse(new URL(url).openStream());
  24. //get the root element
  25. Element docEle = dom.getDocumentElement();
  26. //get a nodelist of elements
  27. NodeList nl = docEle.getElementsByTagName("ReadTripResponse");
  28.  
  29. if (nl != null && nl.getLength() > 0) {
  30. for (int i = 0; i < nl.getLength(); i++) {
  31. //get the employee element
  32. Element el = (Element) nl.item(i);
  33. String firstname = getTextValue(el, "TripID");
  34. //String lastname = getTextValue(el, "lastname");
  35. // String nickname = getTextValue(el, "nickname");
  36. // int salary = getIntValue(el, "salary");
  37.  
  38. System.out.println(firstname);
  39. // System.out.println(lastname);
  40. // System.out.println(nickname);
  41. // System.out.println(salary);
  42. }
  43. }
  44.  
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48.  
  49. }
  50. private static String getTextValue(Element ele, String tagName) {
  51. String textVal = null;
  52. NodeList nl = ele.getElementsByTagName(tagName);
  53. if (nl != null && nl.getLength() > 0) {
  54. Element el = (Element) nl.item(0);
  55. textVal = el.getFirstChild().getNodeValue();
  56. }
  57.  
  58. return textVal;
  59. }
  60. private static int getIntValue(Element ele, String tagName) {
  61. return Integer.parseInt(getTextValue(ele, tagName));
  62. }
  63. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class ReadXMLFromServer is public, should be declared in a file named ReadXMLFromServer.java
public class ReadXMLFromServer {
       ^
1 error
stdout
Standard output is empty