how to store XML if i want to change its values in my service?

hey guys. in my service want to use XML. in the service Id change needed values of my XML tags. how can i do that? i was thinking about storing the XMLsomewhere, and then 'importing'/using it in my service methods. how can i do that? thanks
28 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.
Peter Rader
Peter Rader6mo ago
You could deserialize the xml into java-beans using jaxb.
bambyzas
bambyzasOP6mo ago
but if i want to update the tag values, do i really need to deserialize from XML? e.g. i have <IBAN>123</IBAN>, and would like to have <IBAN>1234</IBAN>
Peter Rader
Peter Rader6mo ago
You do not need beans for that. You could use jsoup having this code: JSoup.parse(xml).select("IBAN").setText(1234);
bambyzas
bambyzasOP6mo ago
oh wow! thats very awesome thing. but i was wondering what are the negative sides of it? bc using jackson/jaxb thing, having pojo for the xml is more or less the standard approach (as i understand)
Peter Rader
Peter Rader6mo ago
Well, you need a little experience about css-selectors. Plain backend-developers might never heard of css-selectors. Using the plain jaxb approach you are byte-true. JSoup does not support byte-trueness.
tjoener
tjoener6mo ago
Jsoup for xml... Have to remember that 👍
bambyzas
bambyzasOP6mo ago
can you rephrase? im not using selectors on my xml file
Peter Rader
Peter Rader6mo ago
I am using a css-selector in JSoup.parse(xml).select("IBAN").setText(1234);
bambyzas
bambyzasOP6mo ago
im trying to do how you showed me, but i dont have setText method
No description
Peter Rader
Peter Rader6mo ago
Ok, my fault. Try : JSoup.parse(xml).select("IBAN").get(0).setText(1234);
bambyzas
bambyzasOP6mo ago
crap 😦
No description
Peter Rader
Peter Rader6mo ago
Try Soup.parse(xml).select("IBAN").get(0).text("1234");
bambyzas
bambyzasOP6mo ago
or maybe this one?
No description
Peter Rader
Peter Rader6mo ago
No. Soup.parse(xml).select("IBAN").get(0).text("1234");
bambyzas
bambyzasOP6mo ago
seems fine
No description
bambyzas
bambyzasOP6mo ago
ill test it in a couple of mins but it changes the value and thats it? i dont need to export or smth like that?
Peter Rader
Peter Rader6mo ago
The contents in the file remains unchanged.
bambyzas
bambyzasOP6mo ago
oh. how do i save the changes?
Peter Rader
Peter Rader6mo ago
Try
var soup = Jsoup.parse(xml);
soup.select("IBAN").get(0).text("1234");
System.out.println(soup);
var soup = Jsoup.parse(xml);
soup.select("IBAN").get(0).text("1234");
System.out.println(soup);
How to read/write file contents is a different thing from this question. You could ask a second question how to save the xml to a file.
bambyzas
bambyzasOP6mo ago
ok. i get
<iban>
123
</iban>
<iban>
123
</iban>
but for some reason its iban, not IBAN
Peter Rader
Peter Rader6mo ago
What is the filename in the variable xml?
bambyzas
bambyzasOP6mo ago
File xml=new File("src/main/resources/accountStatementRequestBody.xml"); Document parsedXml=Jsoup.parse(xml); System.out.println(parsedXml.select("IBAN").get(0).text("123"));
Peter Rader
Peter Rader6mo ago
You print only the iban-node. Dont you like to print the whole xml?
bambyzas
bambyzasOP6mo ago
yeah. but i see that iban node is not in capitals, i get worried lol
Peter Rader
Peter Rader6mo ago
CSS itself is not case-sensitive.
bambyzas
bambyzasOP6mo ago
ive tried googling but i cant find how to save contents to an xml file
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