Home » Developer & Programmer » JDeveloper, Java & XML » split html content by tag (java 1.6)
icon5.gif  split html content by tag [message #554519] Tue, 15 May 2012 02:00 Go to next message
Plymouth_Rock
Messages: 13
Registered: December 2008
Junior Member
I need to modify the content of all html content to be sent as response from our server according to the tag. say for example, only text content inside a td or div tag will be modified. As per solution we have thought the content will be captured in the Filter of the server. Here, we will split the content against tag and modify the desired text, and then merge them again.

So, for example, if my content is,
.....
<tr>
        <td valign="center">Claim Event Code &nbsp;</td>
        <td><input type="text" class="field" name="EventCode"size="20"></td>
        <td valign="center">
		<SELECT name="pClaimStatus" >
			<option value=""selected></option> 
			<option value="A">Notified</option>		
			<option value="B">Opened</option>
.....


then my desired output (may be an array) will be like this --
1. <tr>
2. <td valign="center">
3. Claim Event Code &nbsp;
4. </td>
5. <td>
6. <input type="text" class="field" name="EventCode"size="20">
7. </td>
8. <td valign="center">
9. <SELECT name="pClaimStatus" >
10. <option value=""selected>
11. </option>
12. <option value="A">
13. Notified
14. </option>. .
15. <option value="B">
16. Opened
17. </option>

so that now I can work upon item 3, 13, 16 and so on.

Is this method is ok? if so how could we split and get such array in java.

Thanks in Advance,
Plymouth_Rock
Re: split html content by tag [message #554521 is a reply to message #554519] Tue, 15 May 2012 02:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Not a direct xml/html answer but could be something like:
SQL> with 
  2    data as (
  3      select '<tr>
  4          <td valign="center">Claim Event Code &nbsp;</td>
  5          <td><input type="text" class="field" name="EventCode"size="20"></td>
  6          <td valign="center">
  7    <SELECT name="pClaimStatus" >
  8     <option value=""selected></option> 
  9     <option value="A">Notified</option>  
 10     <option value="B">Opened</option>
 11  .....' val from dual
 12    ),
 13    lines as ( select level line from dual connect by level <= 100 )
 14  select trim(ltrim(rtrim(regexp_substr (val, '>[^<]*</', 1, line), '</'), '>')) res
 15  from data, lines
 16  where trim(ltrim(rtrim(regexp_substr (val, '>[^<]*</', 1, line), '</'), '>')) is not null
 17  /
RES
-----------------------------------------------------------------------------------------------
Claim Event Code &nbsp;
Notified
Opened

3 rows selected.

Regards
Michel
Re: split html content by tag [message #554534 is a reply to message #554521] Tue, 15 May 2012 04:13 Go to previous messageGo to next message
Plymouth_Rock
Messages: 13
Registered: December 2008
Junior Member
Thanks Michel for the thought. But this is not fulfilling the requirement. The reason is I need the whole array of 17 elements as in the example. Then only I can recreate the html after specific modifications.
Re: split html content by tag [message #554536 is a reply to message #554534] Tue, 15 May 2012 04:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
If you posted a real example maybe we can help: what you have and what you want from it.

Regards
Michel
Re: split html content by tag [message #554537 is a reply to message #554536] Tue, 15 May 2012 04:38 Go to previous messageGo to next message
Plymouth_Rock
Messages: 13
Registered: December 2008
Junior Member
for some reason I cannot provide exact example here. but the given one is pretty workable, I believe. In the example I have just taken a small part of a whole html response, so obviously it will have start and end tag of html, body etc., all in right places.

now, say for some specific tags, I need to change the text; say I will append "#". Hence,
1. "Claim Event Code &nbsp;" will become "Claim Event Code &nbsp;#"
2. "Notified" will become "Notified#"
3. "Opened" will become "Opened#"

This modification can be possible if I have the whole array of that 17 elements (see 1st post, please) with me, and I would now be able to append all these elements and send as a complete html response.
Re: split html content by tag [message #554538 is a reply to message #554537] Tue, 15 May 2012 04:54 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
I gave you how to get the elements, it is easy to add a # to these elements.
Without a clear test case, I can't say more.

Regards
Michel

[Updated on: Tue, 15 May 2012 04:55]

Report message to a moderator

Re: split html content by tag [message #554539 is a reply to message #554538] Tue, 15 May 2012 05:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
It could be something like:
SQL> with 
  2    data as (
  3      select '<tr>
  4          <td valign="center">Claim Event Code &nbsp;</td>
  5          <td><input type="text" class="field" name="EventCode"size="20"></td>
  6          <td valign="center">
  7    <SELECT name="pClaimStatus" >
  8     <option value=""selected></option> 
  9     <option value="A">Notified</option>  
 10     <option value="B">Opened</option>
 11  .....' val from dual
 12    )
 13  select regexp_replace (val, '>([^<]+)</', '>\1#</') res
 14  from data
 15  /
RES
--------------------------------------------------------------------------------------
<tr>
        <td valign="center">Claim Event Code &nbsp;#</td>
        <td><input type="text" class="field" name="EventCode"size="20"></td>
        <td valign="center">
  <SELECT name="pClaimStatus" >
   <option value=""selected></option>
   <option value="A">Notified#</option>
   <option value="B">Opened#</option>
.....

Regards
Michel
Re: split html content by tag [message #554547 is a reply to message #554539] Tue, 15 May 2012 08:04 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Any feedback?

Regards
Michel
Re: split html content by tag [message #554548 is a reply to message #554547] Tue, 15 May 2012 08:31 Go to previous message
Plymouth_Rock
Messages: 13
Registered: December 2008
Junior Member
sql soln is quite time consuming as there will be db connection and this process is required for each and every response from server.

I opted an approach. 1st I modify the response with an extra "#" (or could be any special character that can be used as separator) before "<"(tag start) and after ">"(tag end). so now code will look like --

#<tr>#
#<td valign="center">#Claim Event Code &nbsp;#</td>#
#<td>##<input type="text" class="field" name="EventCode"size="20">##</td>#
#<td valign="center">#
#<SELECT name="pClaimStatus" >#
#<option value=""selected>##</option>#
#<option value="A">#Notified#</option>#
#<option value="B">#Opened#</option>#

Now simple Java coding can be used to separate this into an array using "#". consecutive "#"es need to be ignored. so now I can scan the array, check any desired tag and opearte (and modify, too) the next element as I wish. Then I will recreate my response, just concatinating the array.

Using db connection may make the response slower. more over, tag wise opeartion may vary and managing them in sql, I feel, bit complex. But the query was good and I had learnt something new. Thanks Michel.

Previous Topic: invoke plsql package through weblogic
Next Topic: Oracle ADF: Search on Tree Table is not working as expected
Goto Forum:
  


Current Time: Wed Apr 17 19:14:34 CDT 2024