示例#1
0
  public static void main(String[] args) throws JSONException {

    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);

    Book book = new Book();
    book.setAuthor_fname("john");
    book.setAuthor_lname("ray");

    String local_uri = "http://localhost:8080/BookStoreRestService/services/books";
    WebResource service = client.resource(UriBuilder.fromUri(local_uri).build());
    ClientResponse response =
        service.path("getbooks").accept(MediaType.APPLICATION_XML).post(ClientResponse.class, book);

    // to run remote web-service
    /*String remote_uri = "http://bookstore.jainvinita.com/BookStoreRestService/services/books";
    WebResource remote_service = client.resource(UriBuilder.fromUri(
    		remote_uri).build());
    ClientResponse response = remote_service.path("getbooks")
    		.accept(MediaType.APPLICATION_XML)
    		.post(ClientResponse.class, book);*/

    int status = response.getStatus();
    System.out.println("---------status-------------" + status);
    String xml = response.getEntity(String.class);

    ArrayList<Book> bookArray = parseBooksXML(xml);
    System.out.println("-----------bookArray-----------" + bookArray.size());
    for (int i = 0; i <= bookArray.size() - 1; i++) {
      String isbn = bookArray.get(i).getIsbn();
      System.out.println("-------isbn---------" + isbn);
    }
  }
示例#2
0
  public static ArrayList<Book> parseBooksXML(String xml) {
    ArrayList<Book> bookList = new ArrayList<Book>();
    Book book = null;
    try {
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

      InputSource is = new InputSource();
      is.setCharacterStream(new StringReader(xml));

      Document doc = dBuilder.parse(is);
      doc.getDocumentElement().normalize();
      System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
      NodeList nList = doc.getElementsByTagName("book");
      for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        book = new Book();
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nNode;

          book.setAuthor_fname(
              eElement.getElementsByTagName("author_fname").item(0).getTextContent());
          book.setAuthor_lname(
              eElement.getElementsByTagName("author_lname").item(0).getTextContent());
          book.setBook_title(eElement.getElementsByTagName("book_title").item(0).getTextContent());
          book.setYear(
              Integer.parseInt(eElement.getElementsByTagName("year").item(0).getTextContent()));
          book.setPrice(
              Double.parseDouble(eElement.getElementsByTagName("price").item(0).getTextContent()));
          book.setIsbn(eElement.getElementsByTagName("isbn").item(0).getTextContent());

          System.out.println(
              "Author First Name : "
                  + eElement.getElementsByTagName("author_fname").item(0).getTextContent());
          System.out.println(
              "Author Last Name : "
                  + eElement.getElementsByTagName("author_lname").item(0).getTextContent());
          System.out.println(
              "Book Title : "
                  + eElement.getElementsByTagName("book_title").item(0).getTextContent());
          System.out.println(
              "Price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
          System.out.println(
              "Year : " + eElement.getElementsByTagName("year").item(0).getTextContent());
          System.out.println(
              "ISBN : " + eElement.getElementsByTagName("isbn").item(0).getTextContent());
          System.out.println("----------------------------------");
        }
        bookList.add(book);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bookList;
  }