Re: Getting to the root node of an xml document
- From: Mark Space <markspace@xxxxxxxxxxxxxx>
- Date: Fri, 01 Jun 2007 10:49:19 -0700
Rodolphe G wrote:
(Cross posting, sorry...)
Hello there!
I'm trying to get to an xml document root node (named dataroot), I've
tried different things, like :
No idea, and since you did supply a compilable example, it might be hard to guess. I have some old code around here that reads and then prints out an XML file. It's handy because it lets you see the whole structure of the XML file; I think the root node of the tree structure isn't the root node of the document itself. Kinda counter-intuitive that.
This isn't compilable either because I hacked it out of something else, but the code should be working. Just get rid of the JFrame, and replace the JTextArea.append() with println() and you should be good.
public class EditorJFrame extends javax.swing.JFrame
{
private String filename;
//private Map<DefaultMutableTreeNode, org.w3c.dom.Node> sideMenuMap;
/** Creates new form EditorJFrame */
public EditorJFrame()
{
//initComponents();
}
public EditorJFrame( String filename )
{
this.filename = filename;
//initComponents();
// Test for reading an XML file
loadXML();
}
private void loadXML()
{
Document senario = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
senario = parser.parse( filename );
}
catch (Exception ex)
{
System.err.println( ex.getMessage() );
ex.printStackTrace();
}
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Scenario");
preOrderXml( senario, top, 0 );
//SideMenu = new javax.swing.JTree(top);
}
private void preOrderXml( org.w3c.dom.Node docNode, DefaultMutableTreeNode treeNode, int level )
{
org.w3c.dom.NodeList children;
DefaultMutableTreeNode tempTreeNode = treeNode;
// visit current node
jTextAreaTest.append( repeatChar( '\t', level ));
jTextAreaTest.append( docNode.getNodeName() );
jTextAreaTest.append( "\n" );
// for each child, call preOrderXml( child, level+1 )
children = docNode.getChildNodes();
for( int i=0; i<children.getLength(); i++ )
preOrderXml( children.item(i), treeNode, level+1 );
}
private String repeatChar( char c, int count )
{
StringBuffer tempSB = new StringBuffer();
for( int i = 0; i < count; i++ )
tempSB.append( c );
return tempSB.toString();
}
}
.
- Follow-Ups:
- Re: Getting to the root node of an xml document
- From: Mark Space
- Re: Getting to the root node of an xml document
- References:
- Getting to the root node of an xml document
- From: Rodolphe G
- Getting to the root node of an xml document
- Prev by Date: Getting to the root node of an xml document
- Next by Date: Re: Frustrated by AWT..need some help
- Previous by thread: Getting to the root node of an xml document
- Next by thread: Re: Getting to the root node of an xml document
- Index(es):
Relevant Pages
|