I have got two problems with using xml.
First Problem:
This code working correct. If argument have only one child but not list of childs.
doc.getDocumentElement().getChildNodes().item(t).getLastChild().getNodeValue()
I would like to prepare something like this:
If(doc.getDocumentElement().getChildNodes().item(t).getLastChild().getNodeValue() !=null)
but this doesn't work because value is never empty but I need similar If which can check is list of value or not.
Normal Node:
<name>Adam</name>
List Node:
<car>
<tire>yes</tire>
<trunk>yes</trunk>
</car>
Second Problem:
If I make nested ChildNodes I`m not able to take value do you know where is a problem?
Example of node:
<name>Adam</name>
If I wrire this I get "name"
doc.getDocumentElement().getChildNodes().item(t).getChildNodes().item(m).getNodeName()
If I will write this I will get null but I need "Adam"
doc.getDocumentElement().getChildNodes().item(t).getChildNodes().item(m).getNodeName()
answer - I should use ToLenght() > 1
Related
<xxx1 xmlns="hello">
<xxx2>
<xxx3>
<name>rule_1</name>
</xxx3>
</xxx2>
</xxx1>
I select node by "//*[namespace-uri()='hello']/*[local-name()='name']"
It should get //hello:xxx1/xxx2/xxx3/name , and it does.
Now I try to get element . In reality, I don't know how much parent for <name> will get <xxx1>;
I try this code
node.getParent().getNamespaceURI() = "Hello"
and increase getParent() amount to get <xxx1>
But the first time I call <xxx3>.getNamespaceURI() it returns true.
Is the namespace inherited?
How to get the element has or not has xmlns?
Sorry for my question was not clearly.
I'm trying to get the element which is the first declared namespace "hello".
<xxx1 xmlns="hello">
<xxx2>
<xxx3>
this three node which one is contained xmlns="hello", 'cause <xxx2> and <xxx3> was not declare xmlns in the label.
Hello and Welcome to Stack Overflow!
Yes, namespaces are sort of inherited, but the terminology normally used is that, in your example, the <name> element is in the scope of the namespace declaration xmlns="hello", so the <name>element will be in the hello namespace.
With DOM4J, you can test whether an element is in a namespace or not like this:
boolean hasNamespace(Element e) {
return e.getNamespaceURI().length() > 0;
}
If the element is not in any namespace, getNamespaceURI() returns an empty string.
I guess that you want to select the <name> element, but you don't know at which level it be, i.e. how many parents it will have. You can always use this XPath expression:
Node node = doc.selectSingleNode("//*[namespace-uri() = 'foo' and local-name() = 'name']");
Given an xpath to an attribute and a new value, I am looking to update the attribute value to the new value.
I have followed the example here: http://vtd-xml.sourceforge.net/codeSample/cs7.html and come up with the following:
autoPilot.selectXPath(xpath);
modifier.updateToken(vtdNav.getAttrVal(vtdNav.toString(autoPilot.evalXPath())), newContent);
...my tests all pass but perhaps because I am not used to the "tokenized" way that vtd-xml works, it doesn't "feel" right so I am just looking for affirmation that I've done the correct thing.
Your code will work just fine... assume you will call modifier.output().
but it is not optimal...
This statement
modifier.updateToken(vtdNav.getAttrVal(vtdNav.toString(autoPilot.evalXPath())), newContent);
Can be written as
modifier.updateToken(autoPilot.evalXPath()+1, newContent);
Because if the attribute name has an index value of i (!=-1), then the attrinute value is always i+1... as attr val immediately follows an attr name. No conditional check is needed.
In my elasticsearch I want to get all the indices' name of the cluster. How can I do using java?
I search the internet but there's no much useful information.
You can definitely do it with the following simple Java code:
List<IndexMetaData> indices = client.admin().cluster()
.prepareState().get().getState()
.getMetaData().getIndices();
The list you obtain contains the details on all the indices available in your ES cluster.
You can use:
client.admin().indices().prepareGetIndex().setFeatures().get().getIndices();
Use setFeatures() without parameter to just get index name. Otherwise, other data, such as MAPPINGS and SETTINGS of index, will also be returned by default.
Thanks for #Val's answer. According to your method, I use it in my projects, the code is:
ClusterStateResponse response = transportClient.admin().cluster() .prepareState()
.execute().actionGet();
String[] indices=response.getState().getMetaData().getConcreteAllIndices();
This method can put all the indices name into a String array. The method works.
there's another method I think but not tried:
ImmutableOpenMap<String, MappingMetaData> mappings = node.client().admin().cluster()
.prepareState().execute().actionGet().getState().getMetaData().getIndices().
then, we can get the keys of mappings to get all the indices.
Thanks again!
I'm trying to automate my Test Cases using Selenium for an OBIEE application. Now, I need to read a value from a tabular report generated. The problem is, the ID of the last cell where the total is, keeps on changing.
For example- Currently the id is: db_saw_9270_6_1610_0.
After refreshing, the ID becomes something else. The 4 numbers in between (9270) changes. The remaining bit are the same. I'm using the following logic to capture this element:
driver.findElement(By.xpath(".//*[contains(#id, '_6_1610_0')]")).getText();
But, it is returning org.openqa.selenium.NoSuchElementException: Unable to locate element:
Please tell me where did I go wrong and what should I do?
you can try starts-with and substring (as a substitute for xpath 2.0 methdod ends-with):
string xpath = "//*[starts-with(#id, 'db_saw_') and substring(#id, string-length(#id) - 8) = '_6_1610_0']"
driver.findElement(By.xpath(xpath)).getText();
You can try below xpath:-
driver.findElement(By.xpath("//*[starts-with(#id, 'db_saw')]")).getText();
driver.findElement(By.CSSselector("a[id*='_6_1610_0']")).getText();
Note: the a represents a html element. If your id is in a element then you have to replace a by table.
Check this out for more examples with css selector
So say I have an XML file that looks like this:
<Object1s>
<Object1>
<Field1></Field1>
<Object2s>
<Object2>
<Field1a></Field1a>
<Field1b></Field1b>
</Object2>
<Object2>
<Field1a></Field1a>
<Field1b></Field1b>
</Object2>
</Object2s>
</Object1>
<Object1>
<Field1></Field1>
<Object2s>
<Object2>
<Field1a></Field1a>
<Field1b></Field1b>
</Object2>
</Object2s>
</Object1>
</Object1s>
The DOM tutorials I've found have not worked when I try and do the same sort of thing. For instance, I want to be able to separate the Object2s by the Object1 that they are in. When following the example given by DOM tutorials where this type of thing doesn't exist in their XML files, I get all the Object2s that are in any Object1 when I try to find them.
Can someone show me an example that handles something like this?
Okay, figured it out. What I do is use the element I declare for each element, and within that call .getElementsBytagName() to get the elements within that element.