better ways to load xml as a string?

hey guys. i want to load xml as a string. im doing it this way:
String requestBodyTemplate = new String(Files.readAllBytes(Paths.get("src/main/resources/accountStatementRequestBodyTemplate.xml")));
String requestBodyTemplate = new String(Files.readAllBytes(Paths.get("src/main/resources/accountStatementRequestBodyTemplate.xml")));
and some people on the internet told me there are better ways, but they didnt explain to me. can smb help me out? thanks
13 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st6mo ago
You shouldn't assume anything in src/main/resources is a file If you package your application, it will likely not be available any more It is a resource so you should access it as such using getClass().getClassLoader().getResource("accountStatementRequestBodyTemplate.xml")
bambyzas
bambyzasOP6mo ago
Baeldung
Load a Resource as a String in Spring | Baeldung
Learn how to inject the contents of a resource file into our beans as a String, with Spring's Resource class making this very easy.
JavaBot
JavaBot6mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
bambyzas
bambyzasOP6mo ago
but now idk what to use. so many choices
dan1st
dan1st6mo ago
depends on the situation
bambyzas
bambyzasOP6mo ago
can u elaborate?
dan1st
dan1st6mo ago
In most cases, I'd use Class#getResource or similar
bambyzas
bambyzasOP6mo ago
what am i doing wrong?
No description
dan1st
dan1st6mo ago
getClass().getClassLoader().getResource("accountStatementRequestBodyTemplate.xml") or getClass().getClassLoader().getResourceAsStream("accountStatementRequestBodyTemplate.xml")
bambyzas
bambyzasOP6mo ago
i dont understand
dan1st
dan1st6mo ago
You can get an InputStream for something in src/main/resources with that method
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server