Ola, just now I sit in an aeropuerto to go to ESpana ala manana-and try out RENFE.
But for your question: has been asked many times and again I have to disappoint you: since a few yrs, and even more so since covid- there are simply NO trenes going over the frontera between the old Yugoslavija republics-there are now only domestic=INland trenes. And often not that many and not very good. Solo uno excepcion: from Serbije to Montes Negros/Cerna Gora.
If you can live without seeing the cidade blanco (Beograd) then you can go by tren via a roundabout much longer route; Via Zagreb-Budapest-somewhere in Romania (not necessarily Bucuresti)-into BUlgaria to Sofia. But you can coma back only via this way too. You can continue into GReece-partly by bus (have to pay) and then ferry to Italia-and on.
IF you insist on going that way you mention-then its partly bus-you must pay (its not very expensive)-details on seat61.com. But you loose ages of time by doing tren-autobus-walk-tren-just use a direct bus then-much easier and quikcer.