Reputation: 59
I have written a code that can save data into xml. NOW I want to use that stored xml and import its data in a table I have in the form. the problem is: the code is with no error but the table is not field with the xml data :( here is my code:
private void jPanel4ComponentShown(java.awt.event.ComponentEvent evt) {
// TODO add your handling code here:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("D:\\test.xml");
Element root = doc.getDocumentElement();
NodeList nodelist1 = root.getElementsByTagName("FrameDefinitionSection");
String[] st= new String[4];
for(int i=0;i<nodelist1.getLength();i++)
{
Node node=nodelist1.item(i);
st[0]= node.getChildNodes().item(1).getTextContent();
st[1]= node.getChildNodes().item(3).getTextContent();
st[2]= node.getChildNodes().item(5).getTextContent();
st[3]= node.getChildNodes().item(7).getTextContent();
((DefaultTableModel) jTable1.getModel()).addRow(st);
}
}
catch(Exception ex)
{
System.out.print("error");
}
}
and here is my xml file : the table should show the nodes in this xml into fields of table but it does not work ! :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<FrameDefinitionSection>
<FrameNameAndElements>
<FrameName>here is the frame's name</FrameName>
<FrameElements>its element</FrameElements>
</FrameNameAndElements>
<FrameDefinition>
<Definition>the definition of the frame</Definition>
</FrameDefinition>
<FrameExampleSentences>
<ExampleSentences>its example as well</ExampleSentences>
</FrameExampleSentences>
</FrameDefinitionSection>
Upvotes: 0
Views: 5382
Reputation: 859
if you write like following, nodelist1 is null.
NodeList nodelist1 = root.getElementsByTagName("FrameDefinitionSection");
So you need to change like this ,
NodeList nodelist1 = doc.getElementsByTagName("FrameDefinitionSection");
Upvotes: 0
Reputation: 1986
You better use this:
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("D:\\test.xml");
Element root = doc.getDocumentElement();
NodeList nodelist1 =
root.getChildNodes();
String[] st = new String[4];
for (int i = 0; i < nodelist1.getLength(); i++)
{
Node node = nodelist1.item(i);
if (node.getNodeType() == node.ENTITY_NODE) {
st[0] = node.getChildNodes().item(1).getTextContent();
st[1] = node.getChildNodes().item(3).getTextContent();
st[2] = node.getChildNodes().item(5).getTextContent();
st[3] = node.getChildNodes().item(7).getTextContent();
((DefaultTableModel) jTable1.getModel()).addRow(st);
}
}
Upvotes: 1