This week's book giveaway is in the Programmer Certification forum. We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
You fell into the classic trap of thinking you can process XML with simple string operations. Never treat XML as text. Use an XML library to process XML. The Java standard API contains an XML DOM API, or if you need something more advanced, you can use the SAX XML parser.