Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1068589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:18:25+00:00 2026-05-16T20:18:25+00:00

I am trying to learn how to import from XML to JTable. I am

  • 0

I am trying to learn how to import from XML to JTable. I am analyzing the following code in an attempt to understand what is happening. The problem is I am unable to figure out why am I failing to see any values in the JTable. I am sure that the XML is being parsed using the DOMBuilder etc. I have included the code and the XML file that I am using.
Many thanks in advance! P.S. Place the person.xml file into the project folder.

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class XMLInJTable extends AbstractTableModel {
        Vector data;
        Vector columns;

        public XMLInJTable() {
                try {
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        Document doc = db.parse("person.xml");

                        NodeList nl = doc.getElementsByTagName("Name");
                        NodeList n2 = doc.getElementsByTagName("Address");
                        NodeList n3 = doc.getElementsByTagName("ContactNo");
                        NodeList listOfPersons = doc.getElementsByTagName("person");
                        String data1 = "", data2 = "", data3 = "";
                        data = new Vector();
                        columns = new Vector();
                        for (int i = 0; i < listOfPersons.getLength(); i++) {
                                data1 = nl.item(i).getFirstChild().getNodeValue();
                                data2 = n2.item(i).getFirstChild().getNodeValue();
                                data3 = n3.item(i).getFirstChild().getNodeValue();
                                String line = data1 + " " + data2 + " " + data3;
                                StringTokenizer st2 = new StringTokenizer(line, " ");
                                while (st2.hasMoreTokens())
                                        data.addElement(st2.nextToken());
                        }
                        columns.add("");
                        columns.add("");
                        columns.add("");

                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public int getRowCount() {
                return data.size() / getColumnCount();
        }

        public int getColumnCount() {
                return columns.size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
                return (String) data.elementAt((rowIndex * getColumnCount())
                                + columnIndex);
        }

        public static void main(String argv[]) throws Exception {
                XMLInJTable t = new XMLInJTable();
                JTable table = new JTable();
                table.setModel(t);
                JScrollPane scrollpane = new JScrollPane(table);
                JPanel panel = new JPanel();
                panel.add(scrollpane);
                JFrame frame = new JFrame();
                frame.add(panel, "Center");
                frame.pack();
                frame.setVisible(true);
        }
}

person.xml

<?xml version="1.0"  encoding="UTF-8"  standalone="no"?>

<Person>
<Name>Angelina</Name>
<Address>Dehi</Address>
<ContactNo>111111</ContactNo>

<Name>Martina</Name>
<Address>Mumbai</Address>
<ContactNo>222222</ContactNo>

</Person>

Edit* I managed to get it working, it was because the line:

NodeList listOfPersons = doc.getElementsByTagName("person");

contained a small letter p for “persons” whenever it should’ve been a capital P. Now I’ve managed to display a single line of values for Angelina however it fails to display for Martina. Need to figure that out now with some much appreciated help thanks =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:18:26+00:00Added an answer on May 16, 2026 at 8:18 pm

    The problem might be with the person.xml; it should be something like:

    <?xml version="1.0"  encoding="UTF-8"  standalone="no"?>
    
    <Persons>
    
    <Person>
    <Name>Angelina</Name>
    <Address>Dehi</Address>
    <ContactNo>111111</ContactNo>
    </Person>
    
    <Person>
    <Name>Martina</Name>
    <Address>Mumbai</Address>
    <ContactNo>222222</ContactNo>
    </Person>
    
    </Persons>
    

    Otherwise, listOfPersons.getLength() is always 1.

    EDIT2:
    On another note, I suggest you to traverse through the node like the following pseudo code:

    for each <person> in <persons>
      for each child in <person>
        if child tag name is "Name"
          # do something 
        else if child tag name is "Address"
          # do something
        else if child tag name is "ContactNo"
          # do something
    

    This way you would make sure you’re getting the correct details for each person.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.