File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Save XML nodes in a String array for ChooseNodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Save XML nodes in a String array for ChooseNodes" Watch "Save XML nodes in a String array for ChooseNodes" New topic
Author

Save XML nodes in a String array for ChooseNodes

Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Hi guys!

Probably a pretty simple question,but anyways,here it goes:
How in the world do I save my received XML nodes inside a string array?
Basically,I want to use the string array to build a Choose Node where I can select from one of these nodes.
Here´s my code so far:



Maybe it´s really just a small trick I´m missing,but I just can´t quite figure it out yet

Greetings,

Randy
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You need to add all the node names into a String array? Use a List<String> instead and iterate the list of elements and get the node name and add into List.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

And why on earth would you want to store Nodes in a String array?
Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Paul,I know, the idea doesn´t sound that brilliant,right? ;)
Basically,I am working on a XML processing application and one of my features is, to capture all possible nodes and give the user the chance to choose, which of these possible nodes he/she wants to be the root node. So therefore, you can for example just just certain sections inside the XML. And to do that, I figured, I use a Combobox and add all nodes to the Combobox. Or is there a better/easier way to do that?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
I think you are confusing the nature of a Node - when you have created a DOM from an XML document you get a complex data structure of java objects which have Node as the "primary datatype" but may be a variety of subtypes. See the really helpful table in the org.w3c.dom.Node JavaDocs.

Your choice Combobox can only display Strings derived somehow from the Node contents, NOT the same a the underlying Node.

If you have a NodeList - the index in that list will let you locate the real underlying Node.

I think you have a lot of exploration of XML ahead of you - Harold's free online book "Processing XML with Java" may be a big help.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Randy Miller wrote:Paul,I know, the idea doesn´t sound that brilliant,right? ;)


There's a common beginner idea that String is the only suitable way to store data.

Basically,I am working on a XML processing application and one of my features is, to capture all possible nodes and give the user the chance to choose, which of these possible nodes he/she wants to be the root node. So therefore, you can for example just just certain sections inside the XML. And to do that, I figured, I use a Combobox and add all nodes to the Combobox. Or is there a better/easier way to do that?


However the people who wrote JComboBox (I assume that's what you meant) didn't do that. You can store any kind of objects in the JComboBox. So choosing to store String objects, when you really want to allow the user to choose from a list of Element objects, is a design failure.
Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Hi guys!

Sorry, I was sick during the weekend, so I couldn´t reply ;)

Now Paul,what you´re basically saying is,all I have to do is, to create a node list, for example like this:



And I can hand over this list to my JComboBox(yes,that´s exactly what I meant-->Sorry,if it wasn´t clear in the first place) and the user will then be able to choose between all the different nodes/elements?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

That's pretty close to being correct. You can pass a list, or array, of any kind of objects to be used as the model for JComboBox (check the API docs for details). However when the JComboBox goes to display those objects, it uses their toString() method to produce the text to display. You may find that the toString() method of DOM nodes doesn't produce what you would like to see, in which case it isn't as simple as that. To fix that problem, you would wrap the DOM node in some customized object which contains both the node and the text you want to see in the GUI.

Of course that's all based my notion that when the user selects the node from the JComboBox, that's because the user wanted to do something with the node. However your example code didn't show any such thing going on. You just said the user would need to select the node, but you didn't say what for. Really you should design your application before writing the code, rather than the other way around.
Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Hi Paul!

Thanks for your reply,even though it´s quite difficult to entirely follow your answer ;)
So first,why I want to implement this function is pretty simple: Like this,I let the user choose the root node for the currently loaded XML/XSD.
I´m basically coding a programm to analyse and modify a XML and that´s one of it´s, well let´s call it specialties ;)
Therefore, I let the user first choose the XML/XSD he/she wants to use and analyse,which elements the file consists of.
These elements would be written into a list and displayed with the JComboBox. And through this box, the user can then choose his own root node and doesn´t have to use the default one.
Hopefully,my problem is a bit clearer now ;)

And don´t panic, I designed my application first, but this is basically a new tweak to this I just want to add to my already implemented functions ;)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Randy Miller wrote:And through this box, the user can then choose his own root node and doesn´t have to use the default one.


This is my point: if the user wants to choose one of the root nodes, then show the user a list of root nodes. Not a list of strings which you then have to map back to the root nodes, but a list of the root nodes themselves.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Save XML nodes in a String array for ChooseNodes