Re: Getting to the root node of an xml document



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();
}
}
.



Relevant Pages

  • Re: BizTalk 2004 :: merging a multiple root node message as output
    ... If you really have a legacy system that is not using well-formed xml (or ... > I have a typical xml message as input (single root node) and I need to map ... > to a message which contains multiple root nodes. ...
    (microsoft.public.biztalk.general)
  • Re: Extract Strings
    ... Keep in mind it's not well-formed XML (needs a root node), ... XPath query on it, you can iterate the nodes like this: ... > Ok, well this isn't an ordinary string, its the string representation of ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: multiple root nodes
    ... you can't have multiple root nodes in an XML document. ... > everytime I want to do that I either get an error or the first root node ... > textData = xmldoc.createTextNodefilesInDirectory ...
    (comp.lang.java.help)
  • persist xml in class referenced in asp.net page
    ... hello I am putting together a web form which builds an xml file. ... my separate class file ie, Create root node, add elements, add ... but I would rather persist an xmldocument object in the ...
    (microsoft.public.dotnet.framework.aspnet)