예제 #1
0
  private final DbRow readRow(XMLStreamReader sr) throws XMLStreamException {
    expectTag(FIELD_ROW, sr);
    DbRow row = new DbRow();
    while (sr.nextTag() == XMLStreamReader.START_ELEMENT) {
      String elemName = sr.getLocalName();
      String value = sr.getElementText();

      try {
        if (!row.assign(elemName, value)) {
          throw new XMLStreamException(
              "Unexpected element <" + elemName + ">: not one of recognized field names");
        }
      } catch (IllegalArgumentException iae) {
        throw new XMLStreamException(
            "Typed access problem with input '" + value + "': " + iae.getMessage(),
            sr.getLocation(),
            iae);
      }
    }
    return row;
  }
예제 #2
0
  @Override
  public int writeData(OutputStream out, DbData data) throws Exception {
    XMLStreamWriter sw = _staxOutFactory.createXMLStreamWriter(out, "UTF-8");
    sw.writeStartDocument();
    sw.writeStartElement(FIELD_TABLE);
    Iterator<DbRow> it = data.rows();
    while (it.hasNext()) {
      DbRow row = it.next();
      sw.writeStartElement(FIELD_ROW); // <row>

      sw.writeStartElement(DbRow.Field.id.name());
      sw.writeCharacters(String.valueOf(row.getId()));
      sw.writeEndElement();

      sw.writeStartElement(DbRow.Field.firstname.name());
      sw.writeCharacters(row.getFirstname());
      sw.writeEndElement();

      sw.writeStartElement(DbRow.Field.lastname.name());
      sw.writeCharacters(row.getLastname());
      sw.writeEndElement();

      sw.writeStartElement(DbRow.Field.zip.name());
      sw.writeCharacters(String.valueOf(row.getZip()));
      sw.writeEndElement();

      sw.writeStartElement(DbRow.Field.street.name());
      sw.writeCharacters(row.getStreet());
      sw.writeEndElement();

      sw.writeStartElement(DbRow.Field.city.name());
      sw.writeCharacters(row.getCity());
      sw.writeEndElement();

      sw.writeStartElement(DbRow.Field.state.name());
      sw.writeCharacters(row.getState());
      sw.writeEndElement();

      sw.writeEndElement(); // </row>
    }
    sw.writeEndElement();
    sw.writeEndDocument();
    sw.close();
    return -1;
  }