reading all attributes of xml elements at once - java

I have xml file as below
<dashboard DASHBOARD_ID="1" DASHBOARD_IMAGE="" DASHBOARD_NAME="TestDashboard">
<linkedpages>
<pages page_id=1212 pagename=""/>
<report reportid=212 reportname=""/>
</linkedpages>
my need is that I should import these tag attribute velues int o respective table say page table, report table, dashborad table and so on.
I am get the elements and their attributes by
String attribute = child.getAttribute("report_id");
but I need to write n number of such line, and its not generic, i can have variable length of attributes.
So i need to be able to read all attributes of each tag.
How can this be done Please help, Any idea of doing this is appreciated.
Thank You

Try this method : getAttributes()
And an example:
List<String> attributNames = new ArrayList<String>();
if(child.getAttributes() != null){
for (int i = 0; i < child.getAttributes().getLength(); i++) {
attributNames.add(child.getAttributes().item(i).getNodeName());
}
}

String[] attributes = new String[child.getAttributes().getLength()];
for (int i = 0; i < attributes.length; i++) {
attributes[i] = child.getAttributes().item(i).getNodeValue();
}

Related

How to append the id to the url after assigning the id to the String

Scenario: I need to append the id to the url.
What I have done :
I have taken the last id from the table and stored it in a list:
Then I get the text of the id and is Stored in a String.
List<WebElement> id = driver.findElements(By.xpath("(//table[contains(#class,'mat-table')]//tr/td[1])[last()]"));
int rowsize = id.size();
for(int i=0;i<rowsize;i++)
{
String text = id.get(i).getText();
System.out.println("Get the id:"+text);
Then I use that text and append it to the URL
String confirmationURL = "https://test-websites.net/#/email?type=confirm";
String newurl = confirmationURL+"&id=text"; = **This part iam giving the text as id ... which is
wrong and I need to enter the id which I got from the list ....**
driver.get(newurl);
So Basically the url should be like: https://test-websites.net /#/email?type=confirm&id=47474
Can someone pls give inputs on what should be done?
You can create a new list of URLS, and can use add method to append text.
List<WebElement> id = driver.findElements(By.xpath("(//table[contains(#class,'mat-table')]//tr/td[1])[last()]"));
String confirmationURL = "https://test-websites.net/#/email?type=confirm";
List<String> newurls = new ArrayList<String>();
int rowsize = id.size();
for(int i = 0; i < rowsize; i++) {
String text = id.get(i).getText();
System.out.println("Get the id:"+text);
newurls.add(confirmationURL + "&id=" + text);
}
after successfully execution of this code, you'd have a newurls list with URLs ending with id's from //table[contains(#class,'mat-table')]//tr/td[1])[last()] xpath.

How to create nested object and array in parquet file?

How do I create a parquet file with nested fields? I have the following:
public static void main(String[] args) throws IOException {
int fileNum = 10; //num of files constructed
int fileRecordNum = 50; //record num of each file
int rowKey = 0;
for (int i = 0; i < fileNum; ++i) {
Map<String, String> metas = new HashMap<>();
metas.put(HConstants.START_KEY, genRowKey("%10d", rowKey + 1));
metas.put(HConstants.END_KEY, genRowKey("%10d", rowKey + fileRecordNum));
ParquetWriter<Group> writer = initWriter("pfile/scanner_test_file" + i, metas);
for (int j = 0; j < fileRecordNum; ++j) {
rowKey++;
Group group = sfg.newGroup().append("rowkey", genRowKey("%10d", rowKey))
.append("cf:name", "wangxiaoyi" + rowKey)
.append("cf:age", String.format("%10d", rowKey))
.append("cf:job", "student")
.append("timestamp", System.currentTimeMillis());
writer.write(group);
}
writer.close();
}
}
I want to create two fields:
Hobbies which contains a list of hobbies ("Swimming", "Kickboxing")
A teacher object that contains subfields like:
{
'teachername': 'Rachel',
'teacherage':50
}
Can someone provide an example how to do this in Java?
Parquet is columned (mini-storages) key-value storage... I.e. this kind of storage cannot keep nested data, but this storage accepts converting logical types of data to binary format (byte array with header that contains data to understand what kind of convertation should be applied to this data).
I'm not sure about how should you implement your converter, but basically you should work with Binary class as data container and create some converter... sample converter you can find for String data type.

Cell values not getting set for newly added rows : using apache poi

I have a table in a ppt slide. Data to the table will be read from an ArrayList (Each object in the ArrayList will contain one row data) of String[].
If the number of rows in the table is less than the size of the ArrayList, new rows will be added to match the size of the ArrayList.
Problem here is data is getting set for already existing rows but not for the newly added rows. Please find the code snippet below.
Any help/suggestion is much appreciated. Thanks in advance.
XSLFTable table = (XSLFTable) shape;
int noOfDataRows=table.getNumberOfRows()-1;
if(noOfDataRows<ap.size()) {
for(int rws=0;rws<(ap.size()- noOfDataRows);rws++) {
System.out.println(" Adding row");
table.addRow();
}
}
List<XSLFTableRow> rows = table.getRows();
for (int i = 0; i < ap.size(); i++) {
String[] awaitprop={'1','2','3','4','5','6','7','8'};
XSLFTableRow row=rows.get(i+1);
List<XSLFTableCell> cells=row.getCells();
for(int j=0;j<cells.size();j++) {
XSLFTableCell cell=cells.get(j);
cell.clearText();
XSLFTextParagraph paragraph=cell.addNewTextParagraph();
paragraph.setTextAlign(TextAlign.CENTER);
XSLFTextRun textRun=paragraph.addNewTextRun();
textRun.setFontFamily("Calibri");
textRun.setFontSize(11);
textRun.setText(awaitprop[j]);
}
}
With reference to following tutorial, at first you should create cell(s) in the new row, I guess. Then you can put a content there.
XSLFTableRow headerRow = table.addRow();
for(int i = 0; i < 3; i++) {
XSLFTableCell th = headerRow.addCell();
XSLFTextParagraph p = th.addNewTextParagraph();
XSLFTextRun r = p.addNewTextRun();
r.setText("Text");
}
I hope it helps!

IBM Filenet Content Navigator Custom Plugin - GridX row goes blank after editing properties

I'm working on an ICN plugin and having trouble fixing this glitch, every result loads the way it is supposed to on the grid widget but as soon as I make any changes to it on the right panel and save them, the grid seems to reload that row but it goes blank since it isn't loading the attributes bound to the columns I specified on the java code to build the grid.
I'm using the "Chapter 6 Creating a feature with search services and widgets" demo plugin from the ibm redbook as example, but where exactly on this sort of plugin can I make navigator load these custom columns and its attributes I want it to reload after editing?
NOTE:
By default columns I mean these, attributes every row will always have by default:
row.addAttribute("ID", doc.get_Id().toString(), JSONResultSetRow.TYPE_STRING, null, doc.get_Id().toString());
row.addAttribute("className", doc.getClassName(), JSONResultSetRow.TYPE_STRING, null, doc.getClassName());
row.addAttribute("ModifiedBy", doc.get_LastModifier(), JSONResultSetRow.TYPE_STRING, null, doc.get_LastModifier());
row.addAttribute("LastModified", doc.get_DateLastModified().toString(), JSONResultSetRow.TYPE_TIMESTAMP, null, doc.get_DateLastModified().toString());
row.addAttribute("Version", doc.get_MajorVersionNumber() + "." + doc.get_MinorVersionNumber(), JSONResultSetRow.TYPE_STRING, null, doc.get_MajorVersionNumber() + "." + doc.get_MinorVersionNumber());
row.addAttribute("{NAME}", doc.get_Name(), JSONResultSetRow.TYPE_STRING, null, doc.get_Name());
row.addAttribute("ContentSize", doc.get_ContentSize(), JSONResultSetRow.TYPE_INTEGER, null, null);
And by custom I mean something like this, where everything is loaded from an XML file:
ArrayList<PluginProperty> pr = pxs.getResults(contextId);
for (int i = 0; i < pr.size(); i++) {
String id = "{" + i + "}";
String propName = pr.get(i).getName();
String propType = pr.get(i).getType();
String prop = "";
.
.
.
else if (propType.equalsIgnoreCase("StringList")) {
int size = doc.getProperties().get(propName).getStringListValue().size();
for (int j = 0; j < size; j++) {
prop += doc.getProperties().get(propName).getStringListValue().get(j).toString() + "; ";
}
}
else if (propType.equalsIgnoreCase("StringValue")) {
prop = doc.getProperties().get(propName).getStringValue();
}
.
.
.
row.addAttribute(id, prop, JSONResultSetRow.TYPE_STRING, null, prop);
}
After the document information data is saved, openItem is called. This updates the item with the latest data.
The ContentList gets the cell value from the _ModelStore.js getValue method. This method calls item.getDisplayValue to get the value to display in the cell. Most likely either item.getDisplayValue is returning null (or blank) or _ModelStore's getValue is not being called for this attribute.
I would suggest looking at the JSON being returning from openItem to verify it looks complete.
Finally found the issue, apparently ICN does not want you to use custom IDs for your rows.
I was loading a bunch of rows from an XML file and creating rows like this:
ArrayList<PluginProperty> pr = pxs.getResults(contextId);
for (int i = 0; i < pr.size(); i++) {
String id = "{" + i + "}";
String propName = pr.get(i).getName();
String propType = pr.get(i).getType();
String prop = "";
.
.
.
else if (propType.equalsIgnoreCase("StringList")) {
int size = doc.getProperties().get(propName).getStringListValue().size();
for (int j = 0; j < size; j++) {
prop += doc.getProperties().get(propName).getStringListValue().get(j).toString() + "; ";
}
}
else if (propType.equalsIgnoreCase("StringValue")) {
prop = doc.getProperties().get(propName).getStringValue();
}
.
.
.
row.addAttribute(id, prop, JSONResultSetRow.TYPE_STRING, null, prop);
}
Now to make it work I was forced to change the id variable to:
String id = pr.get(i).getName();
So that the ids will look like "Id", "DocumentTitle", "Creator", "DateCreated" and "DateLastModified", exactly like the name of each property instead of "{0}", "{1}", "{2}", etc.
Hope this might be of help to someone else!

How to convert rows of sqlite data to a ArrayList<double[]>()?

I get the data type of data from sqlite database is string. How to put these data into ArrayList()? Thank you!
try below code:-
ArrayList<String> array = new ArrayList<String>();
Levels l = new Levels(getBaseContext()); // level ur table name
l.open();
Cursor c_l = l.selectAll();
for (int i = 0; i < c_l.getCount()+1; i++)
{
array.add(c_l.getString(0)); // getstring 0 means your column
}
c_l.close();
l.close();

Categories

Resources