1

I have a huge xml as follows which I have to assign to a single string. This xml is really huge to so I have to go every line and put " " and + before each line. How can I do that with eclipse's find/replace using regEx.

<Student>
  <name></name>
  <age></age>
  <class></class>
  <section></section>
 // More tags
</Student>

What I want :

public final String studentRequest = "<Student>"
            + " <name></name> "
            + " <age></age> "
            + " <class></class>"
            + " <section></section> "
            + " </Student>" ;

Thanks in advance.

rishi
  • 1,792
  • 5
  • 31
  • 63
  • 3
    If it is in the file then why not simply read content of that file to String? Why do you want to put it as string directly via editor? – Pshemo May 17 '17 at 12:52
  • 1
    match `.*`, replace by `"\0" +` – Aaron May 17 '17 at 12:53
  • Alternatively, You can copy the contents of the xml and paste it inside the quotes in `public final String studentRequest = "";` The IDE will automatically formats it. – ayip May 17 '17 at 12:55
  • Possibly related: http://stackoverflow.com/a/2159931/1393766, but this will also add line separators like `\n` `\r` at the end of each line so I am not sure if that is what you wanted. – Pshemo May 17 '17 at 12:59

1 Answers1

0

Highlight the lines with the text, and remember to put the Scope to "Selected Lines".

Then you search for (.)$ and replace by $1"\+ (or $1\\n"\+ if you need linebreaks), and replace ^(.) by "$1.

This will turn

hello
world
!

into

"hello"+
"world"+
"!"

Add a variable assignment at the first line and a semicolon at the end, and you are done. There might be issues if you text contains special characters, though so try to clean those up first.

Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30