Node existingUserNode = loginDoc.selectSingleNode("/returningUser");
String username = existingUserNode.selectSingleNode("/username").getText();
String password = existingUserNode.selectSingleNode("/password").getText();
for
<?xml version="1.0" encoding="UTF-8"?><returningUser><username>user</username><password>password</password></returningUser>
returns null.
I don't think my xpath is wrong? Or am I using the wrong method?
The syntax should be ./username and ./password... above I am looking one level too high by referencing the root
try this
Node existingUserNode = loginDoc.selectSingleNode("/returningUser");
String username = existingUserNode.selectSingleNode("/username").getNodeValue();
String password = existingUserNode.selectSingleNode("/password").getNodeValue();
Related
We are getting url from JSON Response and which we open in in Chrome.The page loads , there is submit button which we click then it redirect to url as :-
https://www.google.com/AB1234
We need the need to retrieve only "AB1234" value from url.
tried following code to get value ="AB1234"
String url = driver.getCurrentUrl();
int index=url.lastIndexOf("/");
String result = url.substring(0,index);
but here getting initial part of url:https://www.google.com/
You need to call substring function with index +1 .
Try below code :
String url = driver.getCurrentUrl();
int index = url.lastIndexOf("/");
String result = url.substring(index + 1);
To parse a URI, it's likely a good idea to use a URI parser.
Given http://example.com/bar
String path = URI.create(driver.getCurrentUrl()).getPath();
will get you '/bar'.
Given http://example.com/bar/mumble the same code gets '/bar/mumble'. It's unclear from your question whether this is what you want. Nevertheless, you should at least start the parse as above.
Suppose this my Sample XML with Namespaces
<data>
<vnfr:vnfr-catalog xmlns:vnfr="urn:ietf:params:xml:ns:yang:nfvo:vnfr">
<vnfr:vnfr>
<vnfr:id>1234</vnfr:id>
<vnfr:vdur>
<vnfr:id>987</vnfr:id>
<vnfr:id>987</vnfr:id>
<vnfr:management-ip>10.100.100.10</vnfr:management-ip>
</vnfr:vdur>
<vnfr:vdur>
<vnfr:id>567</vnfr:id>
<vnfr:id>567</vnfr:id>
<vnfr:management-ip>10.100.100.11</vnfr:management-ip>
</vnfr:vdur>
</vnfr:vnfr>
</vnfr:vnfr-catalog>
</data>
I have a sample Xpath expression which I use in Java
expression="/data/*[local-name() = 'vnfr-catalog']/*[local-name() = 'vnfr']/*[local-name() = 'id']/*[local-name() = 'vdur']/*[local-name() = 'management-ip']/text()";
Output: 10.100.100.10
But how to Iterate and fetch the 2nd management-ip i.e. 10.100.100.11
I tried the below expression with vdur[2] , but it didn't worked.
expression="/data/*[local-name() = 'vnfr-catalog']/*[local-name() = 'vnfr']/*[local-name() = 'id']/*[local-name() = 'vdur[2]']/*[local-name() = 'management-ip']/text()";
How to fetch the 2nd management IP using expression??????
Your query has two mistakes:
You should not get into id element since it does not contain management-ip, they are on the same level of hierarchy
*[local-name() = 'vdur[2]'] should be replaced with *[local-name() = 'vdur'][2]
Below is my query that seems working:
/data/vnfr:vnfr-catalog/vnfr:vnfr/vnfr:vdur[2]/vnfr:management-ip/text()
This is tested on
<data xmlns:vnfr="asdasdas">
<vnfr:vnfr-catalog>
<vnfr:vnfr>
<vnfr:id>1234</vnfr:id>
<vnfr:vdur>
<vnfr:id>987</vnfr:id>
<vnfr:id>987</vnfr:id>
<vnfr:management-ip>10.100.100.10</vnfr:management-ip>
</vnfr:vdur>
<vnfr:vdur>
<vnfr:id>567</vnfr:id>
<vnfr:id>567</vnfr:id>
<vnfr:management-ip>10.100.100.11</vnfr:management-ip>
</vnfr:vdur>
</vnfr:vnfr>
</vnfr:vnfr-catalog>
</data>
Above is your example but amended a bit since you are not declaring the namespace.
Output:
Text='10.100.100.11'
Tested in https://www.freeformatter.com/xpath-tester.html
Your correct query would look like this:
/data/*[local-name() = 'vnfr-catalog']/*[local-name() = 'vnfr']/*[local-name() = 'vdur'][2]/*[local-name() = 'management-ip']/text()
As Alexey resolve your one issue. below is the answer to getting the length of an attribute
While xxx is the tag prefix, use just count(//Formulas/yyy)
Use count keyword
Source
count of element in XPath
Hope it will help you :)
I'm trying to replace a xml rootnode with a string, but it doesn't allow me.
I was trying to give it as
String str = "SOAP-ENV:Body'.'ns1:creditCardResponse";
I should not repeat SOAP-ENV:Body'.'ns1:creditCardResponse in all these lines.
def rootnode = new XmlParser().parseText(responseXml);
status = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Status.text();
errorCode = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Errorcode.text();
errorInfo = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Errorinfo.text();
referenceCode = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.ReferenceCode.text();
requestIp = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.RequestIP.text()
Any ideas would be greatly appreciated.
Thanks.
Remember that these "paths" are just a series of normal groovy property accesses, so you can store any intermediate point in the path as a variable and continue navigating from there:
deg rtn = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return
status = rtn.Status.text()
errorCode = rtn.Errorcode.text()
// etc.
I am reading mongo collection from java code. When I am trying to read the _id value, I am getting the following:
{"$oid":"541333629520f6e05b0cb410"}
I am reading like: jsonObject.get("_id") from java code. I was expecting something like: "_id" : "541333629520f6e05b0cb410"
Here I am looking for a way so that I can get the _id as a string in one operation.
So far I have been trying the following:
JSONObject idObj = (JSONObject)JSONObj.get("_id");
ObjectId objectId = (ObjectId) idObj.get("$oid");
Workaround this issue using the following snippet:
JSONObject idObj = (JSONObject)obj.get("_id");
String strID = (String) idObj.get("$oid");
May be there are some other way to do this in a better way.
With MongoDb Driver version 3 and using Document object.
Document temp = hwCursor.next();
temp.getObjectId("_id").toString();
or
temp.getObjectId("_id").toHexString();
This worked for me:
String objectId = (String) result.get("_id.$oid");
Maybe there's better ways to do it. Let me know if it works for you.
Cheers!
import org.bson.types.ObjectId;
ObjectId idObj = (ObjectId)obj.get("_id");
String id = idObj.toString()
BsonObjectId bid = (BsonObjectId) result.get("_id");
String str = bId.getValue.toString();
String str_id = JSONObj.getId().asObjectId().getValue().toString());
I have an URL address like: http://myfile.com/File1/beauty.png
I have to remove http://site address/ from main string
That mean result should be File1/beauty.png
Note: site address might be anything(e.g some.com, some.org)
See here: http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html
Just create a URL object out of your string and use URL.getPath() like this:
String s = new URL("http://myfile.com/File1/beauty.png").getPath();
If you don't need the slash at the beginning, you can remove it via s.substring(1, s.length());
Edit, according to comment:
If you are not allowed to use URL, this would be your best bet: Extract main domain name from a given url
See the accepted answer. Basically you have to get a TLD list, find the domain and substract everything till the domain names' end.
If, as you say, you only want to use the standard String methods then this should do it.
public static String getPath(String url){
if(url.contains("://")){
url = url.substring(url.indexOf("://")+3);
url = url.substring(url.indexOf("/") + 1);
} else {
url = url.substring(url.indexOf("/")+1);
}
return url;
}
If the url contains :// then we know that the string you are looking for will come after the third /. Otherwise, it should come after the first. If we do the following;
System.out.println(getPath("http://myfile.com/File1/beauty.png"));
System.out.println(getPath("https://myfile.com/File1/beauty.png"));
System.out.println(getPath("www1.myfile.com/File1/beauty.png"));
System.out.println(getPath("myfile.co.uk/File1/beauty.png"));;
The output is;
File1/beauty.png
File1/beauty.png
File1/beauty.png
File1/beauty.png
You can use the below approach to fetch the required data.
String url = "http://myfile.org/File1/beauty.png";
URL u = new URL(url);
String[] arr = url.split(u.getAuthority());
System.out.println(arr[1]);
Output - /File1/beauty.png
String s = "http://www.freegreatpicture.com/files/146/26189-abstract-color-background.jpg";
s = s.substring(s.indexOf("/", str.indexOf("/") + 1));