开发者

Directly show XML record to jtable record

开发者 https://www.devze.com 2022-12-08 15:22 出处:网络
I am a c++ programmer but new to the java world. I have to display the xml data to a jtable directly.

I am a c++ programmer but new to the java world. I have to display the xml data to a jtable directly.

Say,

the xml is of the following format

<Name> Tom </Name>
<DateofBirth> 12/3/1985 </DateofBirth>
<country> 开发者_Python百科US </country>

Then the table needs to be dispalyed as follows

Name    |     DateofBirth | Country

Tom      12/3/1985       Us

Is it do-able? If so can any one please provide a sample?


Yes, this is very do-able. There are 2 steps to this process. The first is to parse the xml. Some sample Java code for parsing xml would be (this example shows getting a "person" node from the xml file, but it can easily be adapted to your xml file):

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse("xml/sample.xml");
NodeList personNodes = document.getElementsByTagName("person");
List<String> names = new LinkedList<String>();
for (int i = 0; i < personNodes.getLength(); i++) {
  String firstName = null;
  String lastName = null;
  Node personNode = personNodes.item(i);
  NodeList children = personNode.getChildNodes();
  for (int j = 0; j < children.getLength(); j++) {
  Node child = children.item(j);
  String nodeName = child.getNodeName();
  String nodeValue = child.getTextContent();
  if ("firstName".equals(nodeName)) {
    firstName = nodeValue;
  } else if ("lastName".equals(nodeName)) {
   lastName = nodeValue;
  } 
}
names.add(firstName + " " + lastName);
} 

Once you have extracted the data you need, created a new JTable that uses this data. The simplest JTable constructor to use for your purposes would be:

JTable(Object[][] rowData, Object[] columnNames) 

There are more advanced and better ways to do this (such as with data binding frameworks), but this is definitely a good starting point.


It's certainly do-able.

I would check out the JDOM library, which provides a simple API to XML. It's easier to use than the standard but somewhat unwieldy DOM/SAX libraries.

If you have a schema for the above XML, JAXB may be of interest, although it's quite heavyweight.

I can't reliably advise on the Swing/JTable side, however. I'm sure there are lots of people who can look after that side of things.


TableModel is an interface. If you really want to populate your table directly from the XML document, write a custom implementation of the TableModel interface which gets/sets values from your in-memory DOM object.

One advantage of this is that you can support editing the table, and apply the edits directly to your XML elements.

Just be aware that these TableModel methods are called VERY FREQUENTLY and thus should be very fast. If you're dealing with large XML documents, the random-access speeds for getting to an individual cell might not be speedy enough.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号