Example #1
0
  private void writeXMLAttributes(Element node, XmlSerializer serializer, Long recordId) {
    try {
      String nodeName = node.getNodeName();
      if (mySQLiteHelper.tableExists(nodeName)) {
        Cursor cursor = retrieveDatabaseRecordForNode(nodeName, recordId);

        if (cursor.moveToFirst()) {
          // get a map containing the attributes of this node
          NamedNodeMap attributes = node.getAttributes();

          // get the number of nodes in this map
          int numAttrs = attributes.getLength();

          for (int i = 0; i < numAttrs; i++) {
            Attr attr = (Attr) attributes.item(i);
            String attrName = attr.getNodeName();
            try {
              // setting attribute to element
              if (cursor.getColumnIndex(attrName) != -1) {
                serializer.attribute(
                    "", attrName, cursor.getString(cursor.getColumnIndex(attrName)));
              }
            } catch (Exception doNothing) {
              doNothing.printStackTrace();
            }
          }
          cursor.close();
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Example #2
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void writeXML(Element node, XmlSerializer serializer, Long recordId) {
    try {
      String nodeName = node.getNodeName();
      // String nodeContent = node.getTextContent();

      serializer.startTag("", nodeName);

      Cursor cursor = retrieveDatabaseRecordForNode(nodeName, recordId);
      if (cursor.moveToFirst()) {
        writeXMLAttributes(node, serializer, recordId);

        int valueIndex = cursor.getColumnIndex("content");
        if (valueIndex != -1) {
          String value = "";
          if (cursor.getType(valueIndex) == Cursor.FIELD_TYPE_STRING) {
            value = cursor.getString(valueIndex);
          } else if (cursor.getType(valueIndex) == Cursor.FIELD_TYPE_FLOAT) {
            value = String.valueOf(cursor.getFloat(valueIndex));
          } else if (cursor.getType(valueIndex) == Cursor.FIELD_TYPE_INTEGER) {
            value = String.valueOf(cursor.getInt(valueIndex));
          }
          serializer.text(value);
        }

        Long id = cursor.getLong(cursor.getColumnIndex("_id"));

        NodeList entries = node.getChildNodes();
        int num = entries.getLength();
        for (int i = 0; i < num; i++) {
          if (entries.item(i) instanceof Element) {
            Element child = (Element) entries.item(i);

            String fieldName = child.getNodeName();
            if (cursor.getColumnIndex(fieldName) != -1) {

              String childTableName = fieldName;
              String parentTableName = nodeName;
              // check if the field falls under foreign id cols and
              if (mySQLiteHelper.tableExists(childTableName)
                  && mySQLiteHelper.tableContainsColumn(childTableName, parentTableName)) {
                String sqlQuery =
                    "select * from "
                        + childTableName
                        + " where "
                        + parentTableName
                        + " = \""
                        + id
                        + "\"";
                Cursor cursor2 = db.rawQuery(sqlQuery, null);

                if (cursor2.moveToFirst()) {
                  do {
                    Long childId = cursor2.getLong(cursor2.getColumnIndex("_id"));
                    writeXML(child, serializer, childId);
                  } while (cursor2.moveToNext());
                }
                cursor2.close();
              } else { // its not a table
                serializer.startTag("", fieldName);
                String fieldValue = "";
                int columnIdex = cursor.getColumnIndex(fieldName);
                if (cursor.getType(columnIdex) == Cursor.FIELD_TYPE_STRING) {
                  fieldValue = cursor.getString(columnIdex);
                } else if (cursor.getType(columnIdex) == Cursor.FIELD_TYPE_FLOAT) {
                  fieldValue = String.valueOf(cursor.getFloat(columnIdex));
                } else if (cursor.getType(columnIdex) == Cursor.FIELD_TYPE_INTEGER) {
                  fieldValue = String.valueOf(cursor.getInt(columnIdex));
                }
                serializer.text(fieldValue);
                serializer.endTag("", fieldName);
              }
            }
          }
        }
      }

      cursor.close();

      serializer.endTag("", node.getNodeName());

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Example #3
0
 public FormBuilder(Context context) {
   this.context = context;
   mySQLiteHelper = MySQLiteHelper.getInstance(context);
   db = mySQLiteHelper.getWritableDatabase();
 }
Example #4
0
 public Cursor retrieveDatabaseRecordForNode(String node, Long id) {
   String sqlQuery = "select * from " + node + " where _id = " + id;
   Cursor cursor = mySQLiteHelper.executeQuery(sqlQuery);
   return cursor;
 }