Пример #1
0
  private String path(UriComponentsBuilder builder, Article article, boolean encode) {
    Map<String, Object> params = new HashMap<>();
    builder.path("/{year}/{month}/{day}/{code}");
    params.put("year", String.format("%04d", article.getDate().getYear()));
    params.put("month", String.format("%02d", article.getDate().getMonthOfYear()));
    params.put("day", String.format("%02d", article.getDate().getDayOfMonth()));
    params.put("code", article.getCode());

    UriComponents components = builder.buildAndExpand(params);
    if (encode) {
      components = components.encode();
    }
    return components.toUriString();
  }
Пример #2
0
  public static void main(String[] args) {

    // 建立xml文件對象,其儲存在E碟的根目錄下的article.xml檔案
    File xmlFile = new File("D:\\J2EE\\SQL_statement\\article.xml");
    // 宣告JAXBContext上下文對像
    JAXBContext context;
    try {
      // 透過指定映射的類建立上下文
      context = JAXBContext.newInstance(ArticleData.class);
      // 透過上下文建立xml轉化java的對象Unmarshaller
      Unmarshaller u = context.createUnmarshaller();
      // 將xml資料轉換成java對像
      ArticleData data = (ArticleData) u.unmarshal(xmlFile);
      // 獲得所有的article資料
      List<Article> articles = data.getArticle();
      for (Article a : articles) {
        System.out.println("-------------------------");
        System.out.println(a.getAuthor());
        System.out.println(a.getDate());
        System.out.println(a.getEmail());
        System.out.println(a.getTitle());
      }
    } catch (JAXBException e) {
      e.printStackTrace();
    }
  }