I am new to java and trying following example. With the code below, I can print all nodes of a XML file. Now I would like to use Swing and put it out in a simple Swing window.
I understand how to create a window, but how can I connect the console output to a JTextArea
? I searched for it on Stack Overflow, but did not get the right idea.
public static void main(String[] args) throws IOException
{
try
{
File file = new File("dum.xml");
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = documentBuilder.parse(file);
System.out.println("Root element: "+ document.getDocumentElement().getNodeName());
if (document.hasChildNodes())
{
printNodeList(document.getChildNodes());
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
private static void printNodeList(NodeList nodeList)
{
for (int count = 0; count < nodeList.getLength(); count++)
{
Node elemNode = nodeList.item(count);
if (elemNode.getNodeType() == Node.ELEMENT_NODE)
{
// get node name and value
System.out.println("\nZeilenname =" + elemNode.getNodeName()+ " [Anfang]");
System.out.println("Zeileninhalt =" + elemNode.getTextContent());
if (elemNode.hasAttributes())
{
NamedNodeMap nodeMap = elemNode.getAttributes();
for (int i = 0; i < nodeMap.getLength(); i++)
{
Node node = nodeMap.item(i);
System.out.println("Attributname : " + node.getNodeName());
System.out.println("Attributwert : " + node.getNodeValue());
}
}
if (elemNode.hasChildNodes())
{
//recursive call if the node has child nodes
printNodeList(elemNode.getChildNodes());
}
System.out.println("Zeilenname =" + elemNode.getNodeName()+ " [Ende]");
}
}
}