better ways to load xml as a string?
hey guys. i want to load xml as a string. im doing it this way:
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
⌛
This post has been reserved for your question.
Hey @bambyzas! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
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")
thanks. i found this: https://www.baeldung.com/spring-load-resource-as-string
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.
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.
but now idk what to use. so many choices
depends on the situation
can u elaborate?
In most cases, I'd use
Class#getResource
or similarwhat am i doing wrong?
getClass().getClassLoader().getResource("accountStatementRequestBodyTemplate.xml")
or getClass().getClassLoader().getResourceAsStream("accountStatementRequestBodyTemplate.xml")
i dont understand
You can get an
InputStream
for something in src/main/resources
with that method💤
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.