I want to convert the data in the string KeyData (which is present is <eg2keydata> attribute) to *. But it always takes the first instance of keyData in the string msg. How can I get the particular instance everytime I want to convert? I am new to this and I am unable to debug it.I am not sure about about the for loop. Anyone please help me find the mistake.Thanks!!
  const std::string& msg = "<PolicyAction><ManualKeyPolicyAction><TransformData SPI=\"1219546177\"><eg1KeyData Protocol=\"abc\" KeyLength=\"64\" KeyData=\"d789c\"></eg1KeyData><eg2KeyData Protocol=\"hm\" KeyLength=\"64\" KeyData=\"4328a036abfcf5e9\"></eg2KeyData><Tunne<PolicyAction><ManualKeyPolicyAction><TransformData SPI=\"1219546177\"><eg1KeyData Protocol=\"aes-cbc\" KeyLength=\"64\" KeyData=\"d789c\"></eg1KeyData><eg2KeyData Protocol=\"hmac-sha1-96\" KeyLength=\"64\" KeyData=\"4328a03\"></eg2KeyData><SaltData ";
  std::size_t search1 = msg.find("<eg2KeyData");
  const std::string from1 = "KeyData=\"";
  const std::string to1 = "\"></eg2KeyData>";
  std::size_t endpos1 = msg.find(to1);
  std::string s = msg;
  std::size_t l1 = 0;
  if (msg.find("<eg2KeyData") != std::string::npos)
  {
      l1 = msg.find("KeyData=\"",search1);
    if (l1 != 0)
    {
      std::size_t r1 = endpos1 - l1 - from1.length();
      s.replace(l1 + from1.length(), r1, r1, '*');
    }
  }
  std::cout << s;