Node position in xml - Java - java

I would like to prepare function which will give me position of node in xml.
For example:
int NodePosition = doc.getDocumentElement().getChildNodes().item(t).getNodeName().Position()
and in future I would like make some like this (of course this is example):
System.out.println(Node.row[NodePosition].tostring());
Is it possible to make something like this? Or maybe you know some function which make something like this.
For example when I go deeper there is a problem to get position. Because m = 7
doc.getDocumentElement().getChildNodes().item(t).getChildNodes().item(m)
When I try make this (but I have to save position):
System.out.println(doc.getDocumentElement().getChildNodes().item(7).getNodeName());
I get error
java.lang.NullPointerException

To me it looks like you are reinventing the wheel - you are trying to build your own version of XPath - See: http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html
How to select specified node within Xpath node sets by index

Related

How to print tree on pdf in java?

I am trying to print a prefix tree, I already modeled the trie class, I need to print the prefix tree something like:
enter image description here
But I can't figure out how to create it in a perfect way, I'm using the iTextPdf library, but I've only made the Root node, but when trying to create the next one I can't center the text, divide the page or something similar, I wanted to know if any of them have a best solution or have any idea to do it. Thank you

Paste into text area

I tried to make my day easier making a program that will register my self into card game tournaments via selenium webdriver. I am beginner so I was thrilled even though it was just basics aka click here, confirm this, write that but at the end I ran into a problem that I am having trouble solving.
At the end you need to submit decks via deck code. The sendKeys function? (not sure if thats the correct wording) does not work since it actually needs to be pasted into the text area. Whenever you try to "just" write into the box it says its invalid deck code.
driver.findElement(By.xpath("//*[#id=\"react-root\"]/div/div/div[2]/div/div[2]/div/div[4]/div[1]/div/textarea")).click();
driver.findElement(By.xpath("//*[#id=\\\"react-root\\\"]/div/div/div[2]/div/div[2]/div/div[4]/div[1]/div/textarea")).sendKeys(Keys.chord(Keys.CONTROL + "v"));
I went with this, first line just clicking in to the text area and then trying to paste it in (while I copied the deck code manually, not sure how I will solve this since I need to paste 3 different ones, but that is issue for the future) but the code just wont paste and I have no idea where is the catch. I tried the paste function in different text area with the same settings meaning I have something copied in my clipboard and it worked just fine.
Example of the deck code is
" AAECAZICCiT3A94FrtICv/IC9fwC2KAD+KED9KID/KMDCkBWX/4BxAapogPIogPcogPvogPZqQMA "
I hope I provided everything I should have otherwise let me know and I will gladly post more :) Thanks in advance and I hope someone can help me
This might not work because I don't see you explicitly copying the deck code, as you mentioned you did it manually. I also modified your example to not use Keys.chord, as you might not need them here.
To copy the element:
deckCodeWebElement = driver.findElement(locatorToFindDeckCode);
deckCodeWebElement.send_keys(Keys.CONTROL+'a')
time.sleep(1)
deckCodeWebElement.send_keys(Keys.CONTROL+'c')
Then, to paste:
deckCodeWebElementToPaste = driver.findElement(locatorToFindDeckCodeToPaste);
deckCodeWebElementToPaste.send_keys(Keys.CONTROL+'v')
On another note, I recommend shortening your XPaths to relative notation, as the absolute notation you are using is very brittle and breaks very easy. You want to find elements irrespective of their location in the DOM tree.
You can change this:
//*[#id=\\\"react-root\\\"]/div/div/div[2]/div/div[2]/div/div[4]/div[1]/div/textarea
to just this:
//textarea
You may need to query on something like ID, class, or name if there are multiple textarea elements, but there is no need to start at the root node and use div[2] and div[4] unless absolutely necessary.

Java Selenium - How to interact with id-less/class-less element?

I'm trying to interact with a button on a page. Linktext and xpath do not work, there are no classes or combinations of selecting elements and looping through them I can find that work.
Here is the screen shot of the code I'm trying to do a .click()
Please help me how do i achieve the same ?
I think you have 2 options as below. I simplified your example HTML code to smoke test these queries:
Select an element based on its content. The drawback is of course that as soon as "Historical Scans" label changes to something else your query will stop working.
//nav[#id='secondaryNav']//ul[contains(#class, "menu")]//a[normalize-space(.)="Historical Scans"]
(working example on xpath tester http://xpather.com/dqZ7UWvz)
Select an element based on the position on the list. The downside is that it will stop working once this element changes its position.
//nav[#id='secondaryNav']//ul[contains(#class, "menu")]/li[3]/a
(http://xpather.com/rgexHKBB)
Based on my experience you should not rely on any other attributes or elements. Ideally, the best option would be to add ids/classes. Please let us know if this solves your problem.

enterprise architect scripting in java: how to change the way of a connector?

I try to change the way of a connector which connects two elements in an activity diagram. I use the Java API (eaapi.jar) from sparx.
My function to connect two elements:
public void connectTwoElements(Element source, Element target) {
Connector con = source.GetConnectors().AddNew("","ControlFlow");
con.SetSupplierID(target.GetElementID());
con.Update();
source.GetConnectors().Refresh();
}
My goal is to change the way of connector like in the below right scenario. The way of the connector to the target element should have an edge point to create a 90° angle.
I didn't found any attribute of the class Connector to implement that. I expect that I can use a function like:
myConnector.addBetweenPoint(int x, int y);
Maybe anyone can help me:)
Regards,
Phil
EDIT:
After Nizam Mohamed and Uffe helps me below, I modified my method:
public void connectTwoElements(Element source, Element target, String connectorLabel) {
Connector con = source.GetConnectors().AddNew(connectorLabel,"ControlFlow");
con.SetSupplierID(target.GetElementID());
con.Update();
source.GetConnectors().Refresh();
diagram.GetDiagramLinks().Refresh();
//change style of diagram link
Collection<DiagramLink> diagramLinks = diagram.GetDiagramLinks();
for(DiagramLink dl : diagramLinks){
if(dl.GetConnectorID()==con.GetConnectorID()){
dl.SetStyle("Mode=3;TREE=LV;");
dl.Update();
diagram.GetDiagramLinks().Refresh();
break;
}
}
}
It is important to Refresh() the Collection DiagramLinks of the Diagram after add a new Connector because otherwise the DiagramLink isn't available in the Collection DiagramLinks to change the style. Of course you must Refresh() it again after changing the style.
You can set the linestyle to Orthogonal - Square or Lateral - Vertical to achieve this.
To do this, you need to get the DiagramLink and set its style.
Below are some combinations
(for Orthogonal Square) Mode=3;TREE=OS;
(for Lateral Vertical) Mode=3;TREE=LV;
Like elements, whose visual representation in one specific diagram is represented by a DiagramObject, a DiagramLink controls the display of one connector in one diagram. So if you want to change the connector's appearance in one diagram without affecting any others, you need to retrieve the relevant DiagramLink from Diagram.DiagramLinks and make your changes to that.
No method along your suggested lines exists, so the simplest way forward is probably to draw some connectors that look the way you want them and then check the database (t_connector and t_diagramlinks) to see how that geometry is represented.

Using the Java StarTeam API, how can I find which revisions of a StarTeam Item have a certain label?

I'm using the StarTeam java API (v10.4).
I am having a heckuva time trying to find which specific revisions a Label is applied to. What makes this tricky is that there seems to be no method on the Label object which provides a list of what that label is applied to.
So you (seemingly) need to go backwards and find a list of items that you want to check for a particular Label, e.g.
int[] check_these_items = new int[]{ item_to_check.getID() };
int[] labelledItems = myLabel.getLabeledItemIDs(check_these_items)
First, that's kind of convoluted because you need to have a Label in the first place when really what I wanted was to get a list of labels attached to a single revision. But ok, let's go with that for now. So now I need to find a single revision from the history. Let's make this simple and say I want to get the third (or fourth) revision:
Item specific_child = item_to_check.getHistory()[3];
Now the problem is that this new child from history has the same ID (via getID()) as the parent so using getLabeledItemIds(check_these_items) will return the same labels and essentially, we haven't gotten anywhere.
I did find a question related to finding past revisions but it wasn't pertaining to finding labels on past revisions.
Of course, I checked Borland's API documentation but have not come up with the solution.
I know the UI can do it so certainly there is way, I'm just having a time finding it.
Any suggestions even at a high-level would be greatly appreciated!
Once you have the Item and the Label at hand, use getFromHistoryByLabelID(int)
to "Returns the specified version of this item based on the given label ID"
This still doesn't seem like the right answer, but eventually I had to move on and ended up using a workaround of finding the revision date/time of the label I wanted and then comparing the item revision to that. So by that type of reverse logic, I was able to check each label attached to the element and find which revisions had that label.

Categories