I need to create multiple Child Nodes in one element node in XML, do I just append as many times as required to create these nodes? Like this:
rootElement.appendChild(creator);
creator.appendChild(name);
creator.appendChild(email);
creator.appendChild(name);
creator.appendChild(email);
Or does java automatically create the extra child nodes whenever I do this:
name.appendChild(doc.createTextNode("Bob"));
email.appendChild(doc.createTextNode("bob#email.com"));
name.appendChild(doc.createTextNode("Smith"));
email.appendChild(doc.createTextNode("smith#email.com"));
I'm not too sure how it works, any advice or help would be appreciated!
Behavior varies across different implementations, but in general you want to go with the second approach.
When appending or adding a child to a parent the previous parent is replaced. This means that the first approach does nothing but shuffle the same to children. The second approach is correct because you create new children as you go and the previously added children remain untouched by later API calls.
Related
I develop a JavaFX GUI application and I have a problem with a node called "TreeView". My tree should represent a file structure inside a specific directory. The user is supposed to conduct some typical operations on those files like copying, removing or renaming them.
After that happens, I'd like the tree to be refreshed (it means that it is supposed to load once again the structure from the file system). The issue is that, once it is done, the selection on one of the tree items is lost and every directory is collapsed. I would like the selection to be persistent, and all the parent directories to be expanded, even after the content of the tree has been refreshed.
I've tried numerous solutions so far to address this issue, but none of them let me select the same tree item (apart from the deleting operation of course) and expand all needed roots as before. My question is, how can this be achieved? I guess the main problem here is the fact, that I load all the data once again and therefore my new tree items do not match with the "old" ones.
I would appreciate all hints that you can give me in that matter.
I tried to save the item with getSelectionModel() and then set it, it doesn't select the same item as before (it ignores that directories are not expanded).
The web app that I am using has very complex pages with many states. To make this more manageable, I'd like to be able to chunk out major sections of the page into "sub-pages" and tell webdriver to treat them as a relative root of the document so that stray elements don't have the possibility of being picked up. At first I thought that I could use driver.switchTo().frame(WebElement) but that appears be intended for iframes.
Edit: The pages that I'm working with are divided into a couple major nodes, for the sake of example lets say I have a header and body node, which are each huge trees of elements. Instead of telling every WebElement to specifically be a child of one of them, I'd like to create a parent page for things common to both, but then two child page objects that alone represent only the header, and only the body content. This would let me simplify my xpath or css selectors and reduce the possibility of programmer error in having some unwanted nodes showing up in element lookups.
Functions within these page objects would return instances of each other for when it's time for control to move to a different section of the greater page.
which is the most preferable xml document traversal method in java ? Using getElementsByTagName or using TreeWalker .
I've one TreeModel. a Dom Node is the root of the TreeModel. There are two Threads adding nodes to it. One Thread is adding nodes according the nodes added by the other Thread.
e.g.
One Thread adding Nodes named App. The other Thread adding nodes according to the name attribute of the Nodes named App. Sometimes the nodes are not added correctly. The TreeModel only shows the details in the elements by traversing through the nodes.
Note: Adding the App Node is according to the Name attribute of the Node.
Currently for the second Thread, the Nodes are taken by calling getElementsByTagName. Is there any advantage by changing it to TreeWalker ?
I like XPath. W3schools link here, Javadocs here. It is tedious to get started with factories and builders, IMO write your own utility class to save on that tedium. But the syntax to traverse around is expressive and powerful, and it is a "standard" with good documentation.
If you are brave, check out my beta Groovy-like xpath-like project, but I would not propose this as "the most preferable". :-)
ADDED: XPath is a query language for selecting nodes from an XML document. It is good for traversing (moving around in) a DOM structure. However, OP's updated requirements are for manipulating / modifying the DOM structure. XPath is not a good fit there.
I call a webservice and get the following data from it:
Name of the folder
Id of the folder
Id of the parent-folder (null if it is root)
I create ArrayLists (List<String>) for the names, the ids and the parent-ids. So the folder with the name on position "0" has the id and the parent-id on position "0" in these lists.
Now I need to recreate the same structure on my local file system. The user enters a root-directory ("C:\test" for example) that I need to use.
I guess that a recursive method would be the best thing to do, but I have no idea how to implement it.
Any ideas / hints?
I don't see how recursion helps you. I assume you get multiple sets of the data you present, implied by your explanation though you don't say so. You also don't say what order you get them in. I'd create a hashmap, using full path to each parent as a key, and an object representing the directory as a value. The directory object would contain pointers to all its child directories. I'd create that entire hashmap, then walk it top-down. If you don't get the data in the correct order to build it top-down, then you'll have to put them all in a list and search the list to create top-down order, or trust that you can build the list without the IDs and fill them in later
I have a data object represented in a TreeModel, and I'd like to show only part of it in my JTree--for the sake of argument, say the leaves and their parents. How can I hide/filter the unnecessary nodes?
My eventual implementation:
Have two TreeModels, the underlying one and the filtered one.
When a change occurs on the underlying TreeModel, rebuild the filtered TreeModel from scratch. Clone each node that should be visible, and add it to its first visible ancestor in the filtered TreeModel (or the root if none are visible). See teh codez below, if you're curious.
This has the unfortunate side effect of collapsing every path the user had open. To get around this, I added a TreeModelListener to the filtered TreeModel. When the model changes, I save the expanded paths in the JTree (using getExpandedDescendants()), then re-expand them later (using SwingUtilities.invokeLater()).
I had to override equals() in the TreeNode class I was using so that the new cloned nodes would be the same as the old cloned nodes.
...
populateFilteredNode(unfilteredRoot, filteredRoot);
...
void populateFilteredNode(TreeNode unfilteredNode, TreeNode filteredNode)
{
for (int i = 0; i < unfilteredNode.getChildCount(); i++)
{
TreeNode unfilteredChildNode = unfilteredNode.getChildAt(i);
if (unfilteredChildNode.getType() == Type.INVISIBLE_FOLDER)
{
populateFilteredNode(unfilteredChildNode, filteredNode);
}
else
{
TreeNode filteredChildNode = unfilteredChildNode.clone();
filteredNode.add(filteredChildNode);
populateFilteredNode(unfilteredChildNode, filteredChildNode);
}
}
}
You should be aware of GlazedLists. It's a fantastic library for doing complex table transformations with little effort. They've also expanded to trees too. It may require a little refactoring of your existing code to get it into the GlazedLists way of working. But check out the demo and the webcasts to see how powerful it is. (It's one of the essential Swing libraries in my view, and it's open source.)
Have you tried JXTree ? (unfortunately the website is down right now, but you can google for mirrors)
Take a look at this implementation: http://www.java2s.com/Code/Java/Swing-Components/InvisibleNodeTreeExample.htm
It creates subclasses of DefaultMutableNode adding a "isVisible" property rather then actually removing/adding nodes from the TreeModel.
If you're looking for a commercial solution, JideSoft has a filterable treemodel. Other than that, SwingX has a Filter API which'll work on JXTable, JXTreeTable, JXTree, and JXList.
So long as it is still a tree you are displaying, then TreeModel that filters you existing TreeModel should be simple enough.
Leverage the code you use to build your TreeNode(s) and rebuild the TreeNode(s) only including the elements you want. Set the root node on the TreeModel with the filtered root node.