The online racing simulator
[OT] Java - XML > JTree
(7 posts, started )
[OT] Java - XML > JTree
I've been trying for a while today to figure out how to get some simple XML data into a JTree. I've been around the Internet looking at various things, DOM, SAX, the lot, and everything just seems so unnecessarily complex.

I tried a few methods, and got rather close with a couple, but ended up with some problems I couldn't seem to fix

I've never attempted anything DOM-related with Java (most of my stuff has been graphical, or rather simple... as I only bother with Java for my Uni assignments).

Would someone be able to point me in the right direction of:
a) Getting some XML data into a JTree (not every node, just a few levels)
b) Getting XML data in a nice, simple way... in general.

Cheers in advance
If you want to do it properly, then a DOM parser of sorts is the best way to go.

If you don't need the full power of the DOM, and specifically you're just reading the data and not writing XML back out, then a good regular expression should suffice It's a bit crap if the markup changes, but it will work..
Basically, I'm developing a tool (for once, it isn't a uni project... it just as to be cross-platform) for medical students. The XML file consists of a data bank of extended match questions in this sort of format:

<db>
<category name="Top level cat">
<category name="Sub cat">
<emq>
<question>What is 2+2?</question>
<correct>4</correct>
<incorrect>8</incorrect>
<incorrect>3</incorrect>
<incorrect>5</incorrect>
<incorrect>a small goat</incorrect>
</emq>
</category>
</category>
</db>

I was going to use DOM, as I'm also developing a small question bank managing tool, to enable the bank to be extended for future versions of the application, with relative ease.

I looked up several tutorials on how to read XML data through a DOM, but I turned up with some problems. Any chance you can point me in the direction of some good advice on how to go about this with a DOM? Thanks
Unfortunately Java isn't a language I like using myself, so I'm not exactly authoritive on it.

However, I do know of various colleagues and guys n' gals on the 'net who swear by Xerces J (version 1 or 2 I couldn't tell you I'm afraid), and several others who swear by SAX
I recently had to do a Java project and also ran into having to deal with XML. On the .NET side I'd been spoiled by Attribute markup to do serialization of XML to and from objects. I found a project on sourceforge that offered the same for Java and it worked great. Check out http://simple.sourceforge.net/

IMHO, as long as your schema is stable, the serialization path is so much simpler to deal with and cleaner to program in.
James,

Here's a quick and dirty program I threw together that demonstrates what you want, I think. Compile this and run it with the xml filename as the first argument and it will put up a rudimentary JFrame with a tree in it containing nodes populated from the xml file. The key is the Document classes. From there you can get any DOM type information you want from the top level node through it's children.

package sandbox;

import java.io.File;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLDocument {

private String filename;

public ReadXMLDocument(String xmlFilename) {
filename = xmlFilename;
}

public void run() {
try {
File xmlFile = new File(filename);

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(xmlFile);

NodeList nodes = document.getChildNodes();

DefaultMutableTreeNode root = new DefaultMutableTreeNode(xmlFile.getName());

populateNodeFromDOM(root, nodes);

JTree tree = new JTree(root);
JFrame frame = new JFrame();
frame.getContentPane().add(tree);
frame.validate();
frame.pack();
frame.setVisible(true);

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

private void populateNodeFromDOM(DefaultMutableTreeNode parentNode, NodeList domNodes) {
for(int i = 0; i < domNodes.getLength(); i++) {
Node currNode = domNodes.item(i);
String nodeName = null;
if(currNode.getNodeName().startsWith("#")) {
nodeName = currNode.getNodeName() + ": " + currNode.getTextContent();
} else {
nodeName = currNode.getNodeName();
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(nodeName);
populateNodeFromDOM(newNode, currNode.getChildNodes());
parentNode.add(newNode);
}
}

static public void main(String[] args) {
ReadXMLDocument reader = new ReadXMLDocument(args[0]);
reader.run();
}


}

Thanks guys, very much I'll take a look at the various bits and bobs tomorrow, as I'm finished on my code for the night!

[OT] Java - XML > JTree
(7 posts, started )
FGED GREDG RDFGDR GSFDG