I have a long string which contains ~" & "~ etc.
but when I am trying to write it in xml the o/p is: ~"
please suggest a way to write the complete string including "(double quotes)
Below are my code:
for(String str:Parser.queryList){
Element query = doc.createElement("query");
view.appendChild(screen);
screen.appendChild(query);
query.setAttribute("query", str);
}
output: OP =~"=~"
Related
i have simple text im reading from file and I'm getting its content as string
String pomxml = "c:\\foo\\test.json";
String content = FileUtils.readFileToString(file);
the string content (example) is :
"email_settings": {
"email.starttls.enable":"true",
"email.port":"111",
"email.host":"xxxx",
"email.auth":"true",
}
i like to insert new string above "email.host":"xxxx", only if it finds it.
so it will look like :
"email_settings": {
"email.starttls.enable":"true",
"email.port":"111",
"email.name":"myTest",
"email.host":"xxxx",
"email.auth":"true",
}
My question is how to insert this new line into the string
UPDATE
in this example it is JSON , but it can be also simple text or XML file
so i can't rely on JSON providers
You could try doing a regex replacement here:
String input = "\"email_settings\": {\n \"email.starttls.enable\":\"true\",\n \"email.port\":\"111\",\n \"email.host\":\"xxxx\",\n \"email.auth\":\"true\",\n}";
String output = input.replaceAll("([ ]*\"email.host\":\".*?\")", " \"email.name\":\"myTest\",\n$1");
System.out.println(output);
This prints:
"email_settings": {
"email.starttls.enable":"true",
"email.port":"111",
"email.name":"myTest",
"email.host":"xxxx",
"email.auth":"true",
}
However, if you are dealing with proper JSON content, then you should consider using a JSON parser instead. Parse the JSON text into a Java POJO and then write out with the new field.
content = content.replace("\"email.host\":", "\"email.name\":\"myTest\",\n" + " \"email.host\":");
Or you could look in to libraries which parse json files.
I'm using Json Path library to parse JSON. I've following json which has key with space:
{
"attributes": {
"First Name": "Jim",
"Last Name": "Rohn"
}
}
In order to get value of First Name, I wrote code like (where json is object which holds above json) -
String firstName = JsonPath.from(json).getString("attributes.First Name");
But it results into following error -
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found 'Attributes' # line 1, column 67.
.First Name
Could you please suggest how to get values of key having spaces using json-path library?
Try using bracket notation for First Name as follows:
String firstName = JsonPath.from(json).getString("attributes.['First Name']");
UPDATE
Sorry for mixing different JsoPath libraries up.
If you are using com.jayway.jsonpath, try following way for escaping:
DocumentContext jsonContext = JsonPath.parse(json);
String firstName = jsonContext.read("$.attributes.['First Name']");
But if you are using ***.restassured.json-path, please use this one:
String firstName = JsonPath.from(json).getString("attributes.'First Name'");
You have to escape the key with single quotes
Use below code:
String firstName = JsonPath.from(json).getString("'attributes.First Name'");
If you are using io.restassured.path.json.JsonPath library then Escape Sequences is required in the path expression.
String firstName = JsonPath.from(json).getString("attributes.\"First Name\"");
\" <-- Insert a double quote character in the text at this point.
So your path expression looks like (attributes."First Name") and can be parsed by JsonPath library
I'm writing a text to HTML converter.
I'm looking for a simple way to wrap each line of text (which ends with carriage return) with
<p>.....text.....</p>
Can you suggest some String replacement/regular expression that will work in Java ?
Thanks
String txtFileContent = ....;
String htmlContent = "<p>" + txtFileContent.replaceAll("\\n","</p>\\n<p>") + "</p>";
Assuming,
line delimitter is "\n".
One line is one paragraph.
The end of txtFileContent is not "\n"
Hope this help
Try using StringEscapeUtils.escapeHtml and then adding the tags you want at the beginning end.
String escapeHTML = StringEscapeUtils.escapeHtml(inputStr);
String output = "<p>"+escapeHTML+"</p>";
I'm making a java program that would run on a local server.
Server takes request from client using PHP .
<?php
$file = fopen('temp.txt', 'a+');
$a=explode(':',$_GET['content']);
fwrite($file,$a[0].':'.$a[1]. '\n');
fclose($file);
?>
Now I have file "temp.txt" on local server.
Java Program should open the file read line by line and each like should be divided/exploded ":" (In a line there's only one ':' )
I have tried in many ways, but could not get exactly like how PHP splits the line.
Is it possible to use same/similar explode function in JAVA.
Yes, in Java you can use the String#split(String regex) method in order to split the value of a String object.
Update:
For example:
String arr = "name:password";
String[] split = arr.split(":");
System.out.println("Name = " + split[0]);
System.out.println("Password = " + split[1]);
You can use String.split in Java to "explode" each line with ":".
Edit
Example for a single line:
String line = "one:two:three";
String[] words = line.split(":");
for (String word: words) {
System.out.println(word);
}
Output:
one
two
three
I have a requirement in my project.
I generate a comment string in javascript.
Coping Option: Delete all codes and replace
Source Proj Num: R21AR058864-02
Source PI Last Name: SZALAI
Appl ID: 7924675; File Year: 7924675
I send this to server where I store it as a string in db and then after that I retrieve it back and show it in a textarea.
I generate it in javascript as :
codingHistoryComment += 'Source Proj Num: <%=mDefault.getFullProjectNumber()%>'+'\n';
codingHistoryComment += 'Source PI Last Name: <%=mDefault.getPILastName()%>'+'\n';
codingHistoryComment += 'Appl ID: <%=mDefault.getApplId()%>; File Year: <%=mDefault.getApplId()%>'+'\n';
In java I am trying to replace the \n to :
String str = soChild2.getChild("codingHistoryComment").getValue().trim();
if(str.contains("\\n")){
str = str.replaceAll("(\r\n|\n)", "<br>");
}
However the textarea still get populated with the "\n" characters:
Coping Option: Delete all codes and replace\nSource Proj Num: R21AR058864-02\nSource PI Last Name: SZALAI\nAppl ID: 7924675; File Year: 7924675\n
Thanks.
In java I am trying to replace the \n to
Don't replace the "\n". A JTextArea will parse that as a new line string.
Trying to convert it to a "br" tag won't help either since a JTextArea does not support html.
I always just use code like the following to populate a text area with text:
JTextArea textArea = new JTextArea(5, 20);
textArea.setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n0");
// automatically wrap lines
jTextArea.setLineWrap( true );
// break lines on word, rather than character boundaries.
jTextArea.setWrapStyleWord( true );
From here.
Here is a test that works, try it out:
String str = "This is a test\r\n test.";
if(str.contains("\r\n")) {
System.out.println(str);
}
Assuming Javascript (since you try to replace with a HTML break line):
A HTML textarea newline should be a newline character \n and not the HTML break line <br>. Try to use the code below to remove extra slashes instead of your current if statement and replace. Don't forget to assign the value to the textarea after the replacement.
Try:
str = str.replaceAll("\\n", "\n");
I think your problem is here:
if(str.contains("\\n")){
Instead of "\\n" you just need "\n"
Then instead of "\n" you need "\\n" here:
str = str.replaceAll("(\r\n|\n)", "<br>");
By the way, the if(str.contains() is not really needed because it won't hurt to run replace all if there is no "\n" characters.