• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Save XML nodes in a String array for ChooseNodes

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why on earth would you want to store Nodes in a String array?
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic