user1427961
user1427961

Reputation: 179

Add a new string in between existing string

I have read the content of HTML into string, now I need to add a new attribute inside the body tag, I was thinking of using StringBuilder for this. But I am unable to frame the logic. Any help would be really appreciated.

Existing HTML

<body class="temporaryrevision">

HTML that I want to create

<body class="temporaryrevision" bgcolor="#FFFFFF">

Upvotes: 0

Views: 106

Answers (3)

Th3Nic3Guy
Th3Nic3Guy

Reputation: 1979

Ill suggets a way that will be generic:

Use a XML parser for the same.

  1. Load the file into java and pass it to and instance of XML Parser(SAXParser or something like that)
  2. Traverse the parsed Object tree to your required element tag name. in this example HTML
  3. use Library method to add attribute to the element.
  4. convert the object back to xml format(Basic HTML).
  5. Write and replace the file contents with your updated content.

This way is complex but will be generic to all ur such needs..

Upvotes: 0

mgrstnr
mgrstnr

Reputation: 520

You can use jQUery for this:

$( ".temporaryrevision" ).attr( "bgcolor", "#FFFFFF" );

Upvotes: 1

LMK
LMK

Reputation: 2952

String htmlString="<html>...<body class="temporaryrevision">..</body>...</html>";  
String[] tempData=htmlString.split("<body class=/"temporaryrevision/""); 
String data = tempData[0]+"bgcolor=/"#FFFFFF/""+tempData[1];

Upvotes: 2

Related Questions