Пример #1
0
  private static void readProducts() {

    try {
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(fXmlFile);
      NodeList PL = doc.getElementsByTagName("Product");
      List<Product> productList = new ArrayList<Product>();
      for (int temp = 0; temp < PL.getLength(); temp++) {

        Node nNode = PL.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

          Element eElement = (Element) nNode;
          Category category = new Category(getTagValue("ProductCategory", eElement));
          Shop shop = new Shop(getTagValue("ProductShop", eElement));
          Product newProduct =
              new Product.Builder(getTagValue("ProductName", eElement), category, shop).build();
          try {
            newProduct.setItemQuantity(Integer.parseInt(getTagValue("ProductQuantity", eElement)));
            newProduct.setItemUnit(getTagValue("ProductUnit", eElement));
          } catch (Exception e) {
          }
          productList.add(newProduct);
        }
      }

      dc.setProductList(productList);
    } catch (FileNotFoundException e) {
      dc.setProductList(new ArrayList<Product>());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #2
0
  private static void readShoppingLists() {

    String name = "Nada";
    try {
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(fXmlFile);
      NodeList SHL = doc.getElementsByTagName("ShoppingList");
      List<ShoppingList> newshoppingLists = new ArrayList<ShoppingList>();
      for (int temp = 0; temp < SHL.getLength(); temp++) {

        Node nNode = SHL.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nNode;
          name = getTagValue("ShoppingListName", eElement);

          NodeList ShoppingProductList = eElement.getElementsByTagName("ShoppingProduct");

          List<Product> newProductList = new ArrayList<Product>();
          for (int temp1 = 0; temp1 < ShoppingProductList.getLength(); temp1++) {
            Element product = (Element) ShoppingProductList.item(temp1).getChildNodes();
            NodeList Name = product.getElementsByTagName("ProductName");
            String productName = Name.item(0).getTextContent();

            NodeList CatName = product.getElementsByTagName("ProductCategory");
            Category category = new Category(CatName.item(0).getTextContent());
            NodeList SHName = product.getElementsByTagName("ProductShop");
            Shop shop = new Shop(SHName.item(0).getTextContent());
            Product newProduct = new Product.Builder(productName, category, shop).build();
            try {
              NodeList Quan = product.getElementsByTagName("ProductQuantity");
              int quantity = Integer.parseInt(Quan.item(0).getTextContent());
              NodeList Un = product.getElementsByTagName("ProductUnit");
              String unit = Un.item(0).getTextContent();
              newProduct.setItemQuantity(quantity);
              newProduct.setItemUnit(unit);
            } catch (Exception ex) {

            }
            newProductList.add(newProduct);
          }

          newshoppingLists.add(new ShoppingList(name, newProductList));
        }
      }

      dc.setShoppingLists(newshoppingLists);
    } catch (FileNotFoundException e) {
      dc.setShoppingLists(new ArrayList<ShoppingList>());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }