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
⌛
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 could deserialize the xml into java-beans using jaxb.
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>
You do not need beans for that. You could use jsoup having this code:
JSoup.parse(xml).select("IBAN").setText(1234);
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)
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.
Jsoup for xml... Have to remember that 👍
can you rephrase? im not using selectors on my xml file
I am using a css-selector in
JSoup.parse(xml).select("IBAN").setText(1234);
im trying to do how you showed me, but i dont have setText method
Ok, my fault. Try :
JSoup.parse(xml).select("IBAN").get(0).setText(1234);
crap 😦
Try
Soup.parse(xml).select("IBAN").get(0).text("1234");
or maybe this one?
No.
Soup.parse(xml).select("IBAN").get(0).text("1234");
seems fine
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?
The contents in the file remains unchanged.
oh. how do i save the changes?
Try
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.
ok. i get
but for some reason its
iban
, not IBAN
What is the filename in the variable xml?
File xml=new File("src/main/resources/accountStatementRequestBody.xml");
Document parsedXml=Jsoup.parse(xml);
System.out.println(parsedXml.select("IBAN").get(0).text("123"));
You print only the iban-node. Dont you like to print the whole xml?
yeah. but i see that iban node is not in capitals, i get worried lol
CSS itself is not case-sensitive.
ive tried googling but i cant find how to save contents to an xml file
💤
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.