how to insert dynamic row in table of confluence page using java dynamically
Asked by mayank shukla:

i have created one**java program (used main method only)** for **parsing some website** content using **jsoup.** now i want to insert that data into confluence page through my code.i have a separate code which update my page content by new content.but**i want to insert row dynamically with my value in page of confluence**.i have shared my code where i used put( ) method.on the place of this what should i use...

```
public class Main { private static final String BASE_URL = "https://xxxxx.atlassian.net/wiki"; private static final String USERNAME = "xxxxxx"; private static final String PASSWORD = "xxxxxx"; private static final String ENCODING = "utf-8"; private static String getContentRestUrl(final Long contentId, final String[] expansions) throws UnsupportedEncodingException { final String expand = URLEncoder.encode( StringUtils.join(expansions, ","), ENCODING); return String .format("%s/rest/api/content/%s?expand=%s&amp;os_authType=basic&amp;os_username=%s&amp;os_password=%s", BASE_URL, contentId, expand, URLEncoder.encode(USERNAME, ENCODING), URLEncoder.encode(PASSWORD, ENCODING)); } public static void main(final String[] args) throws Exception { final long pageId = 00000000; HttpClient client = new DefaultHttpClient(); // Get current page version String pageObj = null; HttpEntity pageEntity = null; try { HttpGet getPageRequest = new HttpGet(getContentRestUrl(pageId, new String[] { "body.storage", "version", "ancestors" }));            HttpResponse getPageResponse = client.execute(getPageRequest); pageEntity = getPageResponse.getEntity(); pageObj = IOUtils.toString(pageEntity.getContent()); } finally { if (pageEntity != null) { EntityUtils.consume(pageEntity); } } // Parse response into JSON JSONObject page = new JSONObject(pageObj); page.getJSONObject("body") .getJSONObject("storage") .put("value","removing my existing data and set new one"); int currentVersion = page.getJSONObject("version").getInt("number"); page.getJSONObject("version").put("number", currentVersion + 1); // Send update request HttpEntity putPageEntity = null; try { HttpPut putPageRequest = new HttpPut(getContentRestUrl(pageId, new String[] {})); StringEntity entity = new StringEntity(page.toString(), ContentType.APPLICATION_JSON); putPageRequest.setEntity(entity); HttpResponse putPageResponse = client.execute(putPageRequest);         putPageEntity = putPageResponse.getEntity(); } finally { EntityUtils.consume(putPageEntity); } }}
```


Answers:
author: J D
body: Hey, do you think of something like this?

```
// List		ArrayList&lt;String[]&gt; people = new ArrayList&lt;String[]&gt;();		people.add(new String[] { "Alice", "Director", "San Diego", "23" });		people.add(new String[] { "Bob", "Chairman", "Austin", "24" });		people.add(new String[] { "Darth", "Sith Lord", "Las Vegas", "56" });		String middleText = null;		for (String[] person : people) {			middleText = middleText + "&lt;tr&gt;" + "&lt;td&gt;" + person[0] + "&lt;/td&gt;"					+ "&lt;td&gt;" + person[1] + "&lt;/td&gt;" + "&lt;td&gt;" + person[2]					+ "&lt;/td&gt;" + "&lt;td&gt;" + person[3] + "&lt;/td&gt;" + "&lt;/tr&gt;";		}		String text = "&lt;body&gt;"				+ "&lt;h1&gt;Products&lt;/h1&gt;"				+ "&lt;table&gt;"				+ "&lt;tr&gt;"				+ "&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Position&lt;/th&gt;&lt;th&gt;Residence&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;"				+ "&lt;/tr&gt;" + middleText + "&lt;/table&gt;" + "&lt;/body&gt;" + "&lt;p&gt; &lt;/p&gt;"				+ "&lt;p&gt; &lt;/p&gt;" + "&lt;p&gt;"				+ "&lt;ac:structured-macro ac:name='gallery'&gt;"				+ "&lt;ac:parameter ac:name='columns'&gt;1&lt;/ac:parameter&gt;"				+ "&lt;/ac:structured-macro&gt;" + "&lt;/p&gt;";
```

```
page.getJSONObject("body").getJSONObject("storage").put("value", text);
```

comments: author: mayank shukla
body: Thank you so much J.D.i really appreciate your help.now i am able to insert rows dynamically in table.this code will be run as a schedule job to get some data from some sites and put in this table as a week based or day to day job.my problem is suppose first day i run my code and it get 10 records and put in table in 10 rows.whenn next day again i will run my code and if it will get 10 more records.than i want that this 10 record will add after old 10 records.if you give any sample code like above it will very helpfull.again Thanks for your help.


Tags: cloud

confluence-cloud