Friday, 18 July 2014

Java display elements within a single JTable Cell



I am trying to make my own XML Editor and I am using a JTable to display the XML nodes. However, I have the following XML:



<Department id="A">
<DeptCode>Example1</DeptCode>
<DeptName>Example2</DeptName>
<li>List1</li>
<li>List2</li>
<li>List3</li>
<li>List4</li>
<li>List5</li>
<li>-Point</li>
</Department>
<Department id="B">
<DeptCode>Example1</DeptCode>
<DeptName>Example2</DeptName>
<li>List1</li>
<li>List2</li>
<li>List3</li>
<li>List4</li>
</Department>


...and so forth.


How would i go about displaying the "li" elements for each Department on a different row (in JTable) within a single cell? Hope the makes sense!


The closest i could get is this:



NodeList getElementTagName_LI = doc.getElementsByTagName("li");
NodeList getElementTagName_dept = doc.getElementsByTagName("Department");
//GET DEPARTMENTS
for (int a = 0; a < getElementTagName_dept.getLength(); a++) {
org.w3c.dom.Node allElements_dept = getElementTagName_dept.item(a);

if (allElements_dept.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
org.w3c.dom.Element eElement = (org.w3c.dom.Element) allElements_dept;
departmentID = eElement.getAttribute("id").toString();
deptCode = eElement.getElementsByTagName("DeptCode").item(0).getTextContent().toString();
deptName = eElement.getElementsByTagName("DeptName").item(0).getTextContent().toString();
//Get Each XML List Element
li = eElement.getElementsByTagName("li").item(0).getTextContent().toString();
li1 = eElement.getElementsByTagName("li").item(1).getTextContent().toString();
li2 = eElement.getElementsByTagName("li").item(2).getTextContent().toString();
li3 = eElement.getElementsByTagName("li").item(3).getTextContent().toString();
li4 = eElement.getElementsByTagName("li").item(4).getTextContent().toString();

}
//Insert the XML Node Values within the JTable.
dept_TableModel.insertRow(0, new Object[]{departmentID});
dept_TableModel.setValueAt(deptCode, 0, 1);
dept_TableModel.setValueAt(deptName, 0, 2);
dept_TableModel.setValueAt(li+li1+li2+li3+li4, 0, 3);
}


It kind of works;only if there is the same amount of list elements within the XML document for each department. But i need to make it so that it can have as many list elements as needed.


If anyone could point me in the right direction that would be much appreciated, I have looked everywhere on google and have come up with the code for this. I tried multiple array lists but just cant get it to order correctly within the JTable.


Many thanks! Alpaxj


No comments:

Post a Comment