0

I'm not the best english speaker, so I hope I'll be understandable.

I'm looking for the best way to replace a line in a multiline string.

I know that in my case, handling it as an xml may be easier, but sadly I can't for different reasons, I've to consider my string just as a mulitline string with a xml syntax. First of all, I've to find the select_numserie line I want to replace. For this I've just the hote_carte attribute.

In my head, I would copy in another string everything before the substring I wanna replace, then I insert my new line and I copy what's after my substring. But I've no idea how to do this... And no idea how to find the substring I wanna replace..

Here's a string example :

<NewDataSet>
  <Hote>
    <nbpassage_numseries />
    <select_numserie />
    <hote_id>3348</hote_id>
    <hote_carte>265</hote_carte>
    <hote_elenom>AAA</hote_elenom>
    <hote_elepre>Aid2 Aethys1</hote_elepre>
    <division_divcod>6e1_1</division_divcod>
    <regime_lib>DP</regime_lib>
    <hote_carte_codee>32855869</hote_carte_codee>
  </Hote>
  <Hote>
    <nbpassage_numseries />
    <select_numserie />
    <hote_id>3347</hote_id>
    <hote_carte>266</hote_carte>
    <hote_elenom>AAA</hote_elenom>
    <hote_elepre>Aidé Aethys2</hote_elepre>
    <division_divcod>6e1_1</division_divcod>
    <regime_lib>DP</regime_lib>
    <hote_carte_codee>40616617</hote_carte_codee>
  </Hote>
  <Hote>
    <nbpassage_numseries />
    <select_numserie />
    <hote_id>3349</hote_id>
    <hote_carte>7777</hote_carte>
    <hote_elenom>AAA</hote_elenom>
    <hote_elepre>Aidx</hote_elepre>
    <division_divcod>TEST GHISLAIN</division_divcod>
    <regime_lib>Externe</regime_lib>
    <hote_carte_codee>33047084</hote_carte_codee>
  </Hote>
  <Hote>
    <nbpassage_numseries />
    <select_numserie />
    <hote_id>3283</hote_id>
    <hote_carte>7778</hote_carte>
    <hote_elenom>AAA</hote_elenom>
    <hote_elepre>Elva</hote_elepre>
    <division_divcod>TEST GHISLAIN</division_divcod>
    <regime_lib>DP</regime_lib>
    <hote_carte_codee>45390356</hote_carte_codee>
  </Hote>
  <Hote>
    <nbpassage_numseries />
    <select_numserie />
    <hote_id>3284</hote_id>
    <hote_carte>267</hote_carte>
    <hote_elenom>AAA</hote_elenom>
    <hote_elepre>Elvb</hote_elepre>
    <division_divcod>6e2</division_divcod>
    <regime_lib>Externe</regime_lib>
    <hote_carte_codee>23722768</hote_carte_codee>
  </Hote>
 [...]
</NewDataSet>

[EDIT]

It is this line : <select_numserie /> I want to replace.

For exemple, if I have 7778 as parameter,, I've to replace the <select_numserie /> line, in the Hote node, where hote_carte == 7778. And I will replace this by <select_numserie> [timestamp value, this value is given in parameter] </select_numserie>

And in the case where the select_numserie value is already written, then I've to remove the value.

And all of this, without using any XML property or whatever. Just consider the string as a string with an xml syntax.

Alain Bara
  • 19
  • 7
  • Does this answer your question? [How to replace all occurrences of a string?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string) – balint Jun 03 '20 at 09:03
  • No, I'm looking for replacing a specific line – Alain Bara Jun 03 '20 at 09:06
  • does the line means ""? If so, and you have the xml as a string, you can replace it with javascript's regex - after you can re-parse as xml if needed. – balint Jun 03 '20 at 09:10
  • you can use this pattern to identify the opening and closing of outer tag "]*>(.*?)" and then removing inner tag line by using the above regex in str.replace() function. – Atlas Bravoos Jun 03 '20 at 09:17
  • Perhaps if available, using a parser would be a better option. – The fourth bird Jun 03 '20 at 09:48
  • Yes it would be... But I can't, I've to handle it as a string – Alain Bara Jun 03 '20 at 12:35

0 Answers0