-
-
Save anonymous/c345f24d389f3ce75ffd to your computer and use it in GitHub Desktop.
XML to JTree with fixed nodes: attributes are leaves now
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Uses code from Ibbtek <http://ibbtek.altervista.org/XML2JTree> under MIT | |
* license. | |
*/ | |
package xml; | |
import java.awt.BorderLayout; | |
import java.awt.GridLayout; | |
import java.io.IOException; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTree; | |
import javax.swing.tree.DefaultMutableTreeNode; | |
import javax.swing.tree.DefaultTreeModel; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.NamedNodeMap; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
public class XmlToJtree { | |
private Document document; | |
private DefaultTreeModel treeModel; | |
private JTree treeView; | |
public XmlToJtree(String filePath) { | |
if (filePath != null) { | |
document = getDomFromFile(filePath); | |
} | |
if (document != null) { | |
treeModel = new DefaultTreeModel(buildTreeNode(document)); | |
} | |
if (treeModel != null) { | |
treeView = new JTree(treeModel); | |
} | |
} | |
private DefaultMutableTreeNode buildTreeNode(Node rootNode) { | |
DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode( | |
rootNode.getNodeName()); | |
if (rootNode.hasAttributes()) { | |
NamedNodeMap attributes = rootNode.getAttributes(); | |
for (int j = 0; j < attributes.getLength(); j++) { | |
String attr = attributes.item(j).toString(); | |
treeNode.add(new DefaultMutableTreeNode("@" + attr)); | |
} | |
} | |
if (rootNode.hasChildNodes()) { | |
NodeList children = rootNode.getChildNodes(); | |
for (int i = 0; i < children.getLength(); i++) { | |
Node node = children.item(i); | |
short nodeType = node.getNodeType(); | |
if (nodeType == Node.ELEMENT_NODE) | |
treeNode.add(buildTreeNode(node)); | |
else if (nodeType == Node.TEXT_NODE) { | |
String text = node.getTextContent().trim(); | |
if (!text.equals("")) | |
treeNode.add(new DefaultMutableTreeNode(text)); | |
} else if (nodeType == Node.COMMENT_NODE) { | |
String comment = node.getNodeValue().trim(); | |
treeNode.add(new DefaultMutableTreeNode("#" + comment)); | |
} | |
} | |
} | |
return treeNode; | |
} | |
private Document getDomFromFile(String filePath) { | |
Document document = null; | |
try { | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
document = dbf.newDocumentBuilder().parse(filePath); | |
document.normalizeDocument(); | |
} catch (IOException | ParserConfigurationException | SAXException e) { | |
e.printStackTrace(); | |
} | |
return document; | |
} | |
public void showPopUp(String title, int width, int height) { | |
if (treeView != null) { | |
JFrame frame = new JFrame(); | |
frame.setTitle(title); | |
frame.setSize(width, height); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLocationRelativeTo(null); | |
frame.add(new JScrollPane(treeView)); | |
frame.add(new JPanel(new GridLayout(1, 1)), BorderLayout.SOUTH); | |
frame.setVisible(true); | |
} | |
} | |
public static void main(String[] args) { | |
XmlToJtree x2j = new XmlToJtree("simple.xml"); | |
x2j.showPopUp("XML", 400, 600); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment