sith j
sith j

Reputation: 23

Facing problem while nesting XML tags in java

I am trying to create an XML file in Java, I am expecting to get the following output:

<Row>
  <customer_name> foo </customer_name>
  <address>
     <street_address> 123 Hemming Way </street_address>
     <city> Brandon </city>
  </address>
  <region> Manitoba </region>
</Row>

The output I am getting is without the nesting of <address> tag like this:

<Row>
   <customer_name> foo </customer_name>
   <street_address> 123 Hemming Way </street_address>
   <city> Brandon </city>
   <region> Manitoba </region>
</Row>

I don't know how to include separate nesting of <address> for street_address and city. My code is as follows:

ResultSet rs = statement.executeQuery("Select  customer_name,street_address,city, region  from customers ");
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();

while (rs.next()) {
    Element row = doc.createElement("Row");
    results.appendChild(row);
    for (int i = 1; i <= colCount; i++) {
        String columnName = rsmd.getColumnLabel(i);
        Object value = rs.getObject(i);
        Element node = doc.createElement(columnName);
        node.appendChild(doc.createTextNode(value.toString()));
        row.appendChild(node);
    }
}

Upvotes: 0

Views: 37

Answers (1)

Andreas
Andreas

Reputation: 159096

You can't, if you only write generic loop for all columns. You need to write more explicit code, so the street_address and city are nested inside an address tag.

I'd suggest you eliminate the column loop, and write specific code for each column.

Helper methods may be a good idea:

static Element addElement(Element parent, String name) {
    Element elem = parent.getOwnerDocument().createElement(name);
    parent.appendChild(elem);
    return elem;
}

static void addElementValue(Element parent, String name, String value) {
    addElement(parent, name).appendChild(parent.getOwnerDocument().createTextNode(value));
}

It is now easier to write the logic:

while (rs.next()) {
    Element row = addElement(results, "Row");
    addElementValue(row, "customer_name", rs.getString("customer_name"));
    Element address = addElement(row, "address");
    addElementValue(address, "street_address", rs.getString("street_address"));
    addElementValue(address, "city", rs.getString("city"));
    addElementValue(row, "region", rs.getString("region"));
}

This also makes it easier for you to name the elements better, if you so choose, e.g.

while (rs.next()) {
    Element row = addElement(results, "Customer");
    addElementValue(row, "Name", rs.getString("customer_name"));
    Element address = addElement(row, "Address");
    addElementValue(address, "Street", rs.getString("street_address"));
    addElementValue(address, "City", rs.getString("city"));
    addElementValue(row, "Region", rs.getString("region"));
}

to create the following XML:

<Customer>
    <Name>foo</Name>
    <Address>
        <Street>123 Hemming Way</Street>
        <City>Brandon</City>
    </Address>
    <Region>Manitoba</Region>
</Customer>

Upvotes: 1

Related Questions