Pawan
Pawan

Reputation: 32321

how to do a substring of a string in this case

I am getting the following xml response from a third party server

<t0>

<t9>0</t9>

<t1>
<t15>DFT</t15>
<t3>232.15</t3>
<t6>70</t6>
<t4>-3.60</t4>
<t5>-1.53</t5>
<t7>236.10</t7>
<t8>237.10</t8>
<t10>227.50</t10>
<t11>235.75</t11>


<t2>Chimera</t2>

<t58>0.00</t58>
<t59>0.00</t59>



<t12>231.72</t12>
<t19>0</t19>
<t20>4743</t20>
<t13>4809774</t13>
<t17>11145.21</t17>
<t14>04/03/2014 15:59:16</t14>



<t25>0</t25>
<t26>0</t26>
<t27>0</t27>
<t28>0</t28>
<t29>0</t29>

<t30>0</t30>
<t31>0</t31>
<t32>0</t32>
<t33>0</t33>
<t34>0</t34>

<t35>0.00</t35>
<t36>0.00</t36>
<t37>0.00</t37>
<t38>0.00</t38>
<t39>0.00</t39>

<t40>35</t40>
<t41>0</t41>
<t42>0</t42>
<t43>0</t43>
<t44>0</t44>

<t45>4743</t45>
<t46>0</t46>
<t47>0</t47>
<t48>0</t48>
<t49>0</t49>

<t50>232.15</t50>
<t51>0.00</t51>
<t52>0.00</t52>
<t53>0.00</t53>
<t54>0.00</t54>

</t1>



<t1>
<t2>Chimera</t2>
<t15>DSF</t15>
<t3>232.25</t3>
<t6>25</t6>
<t4>-3.45</t4>
<t5>-1.46</t5>
<t7>236.90</t7>
<t8>237.50</t8>
<t10>228.00</t10>
<t11>235.70</t11>
<t12>231.72</t12>
<t19>0</t19>
<t20>0</t20>
<t13>699076</t13>
<t17>1619.90</t17>
<t14>03/04/2014 16:55:19</t14>

<t58>0.00</t58>
<t59>0.00</t59>


<t25>0</t25>
<t26>0</t26>
<t27>0</t27>
<t28>0</t28>
<t29>0</t29>

<t30>0</t30>
<t31>0</t31>
<t32>0</t32>
<t33>0</t33>
<t34>0</t34>

<t35>0.00</t35>
<t36>0.00</t36>
<t37>0.00</t37>
<t38>0.00</t38>
<t39>0.00</t39>

<t40>0</t40>
<t41>0</t41>
<t42>0</t42>
<t43>0</t43>
<t44>0</t44>

<t45>0</t45>
<t46>0</t46>
<t47>0</t47>
<t48>0</t48>
<t49>0</t49>

<t50>0.00</t50>
<t51>0.00</t51>
<t52>0.00</t52>
<t53>0.00</t53>
<t54>0.00</t54>

</t1>



</t0>

I need to take only the first t1 data and eliminate the remaining that is the output should like

<t1>
<t15>DFT</t15>
<t3>232.15</t3>
<t6>70</t6>
<t4>-3.60</t4>
<t5>-1.53</t5>
<t7>236.10</t7>
<t8>237.10</t8>
<t10>227.50</t10>
<t11>235.75</t11>


<t2>Chimera</t2>

<t58>0.00</t58>
<t59>0.00</t59>



<t12>231.72</t12>
<t19>0</t19>
<t20>4743</t20>
<t13>4809774</t13>
<t17>11145.21</t17>
<t14>04/03/2014 15:59:16</t14>



<t25>0</t25>
<t26>0</t26>
<t27>0</t27>
<t28>0</t28>
<t29>0</t29>

<t30>0</t30>
<t31>0</t31>
<t32>0</t32>
<t33>0</t33>
<t34>0</t34>

<t35>0.00</t35>
<t36>0.00</t36>
<t37>0.00</t37>
<t38>0.00</t38>
<t39>0.00</t39>

<t40>35</t40>
<t41>0</t41>
<t42>0</t42>
<t43>0</t43>
<t44>0</t44>

<t45>4743</t45>
<t46>0</t46>
<t47>0</t47>
<t48>0</t48>
<t49>0</t49>

<t50>232.15</t50>
<t51>0.00</t51>
<t52>0.00</t52>
<t53>0.00</t53>
<t54>0.00</t54>

</t1>

I tried this way but this wil fail if its a different symbol

 int a = abc.indexOf("<t1>");
          int b = abc.indexOf("</t1>");

          System.out.println(a);
          System.out.println(b);
          String newstr = abc.substring(24, 848);

My plan is that once i substring this , i wil set this data into a java Object using STAX API

Please let me know if there is any better way to do this ??

Upvotes: 1

Views: 72

Answers (4)

borjab
borjab

Reputation: 11655

Why don't you try to use Xpath. It was designed especifically for this task. The Xpath expression should be something like "//t1[first()]".

It will be much more robust for cases like:

  • <t1 >
  • <t1 attribute="foo">
  • <![CDATA[some user just tiped <t1> in the code ]]>

See: How to read XML using XPath in Java

Upvotes: 0

Jean Logeart
Jean Logeart

Reputation: 53819

Using Jsoup:

String firstT1 = Jsoup.parse(xml).getElementsByTag("t1").first().outerHtml();

Upvotes: 3

Michael Borgwardt
Michael Borgwardt

Reputation: 346300

My plan is that once i substring this , i wil set this data into a java Object using STAX API

Wrong plan. Why the hell would you use error-prone manual parsing when you're planning to pass the result to a real parser right after?

Pass the whole String into StAX and extract only the elements you want. StAX is a streaming API, so you will not actually save memory by pre-processing the string.

Upvotes: 0

user3360944
user3360944

Reputation: 558

I generally use split instead of substring when parsing for xml tags.

Something like this should work:

abc.split("<t1>")[1].split("</t1>")[0];

Upvotes: 0

Related Questions