OPTIMUS
OPTIMUS

Reputation: 692

How to update/insert data in existing node in XML file?

I am having a xml file which consists of certain data

<CREDENTIALS>
<MEMBER_BENEFITS use="yes">
<USERNAME>12345</USERNAME>
<PASSWORD>password</PASSWORD>
</MEMBER_BENEFITS>
<ARTICLE_DOWNLOAD use="yes">
<USERNAME>56789</USERNAME>
<PASSWORD>password</PASSWORD>
</ARTICLE_DOWNLOAD>
</CREDENTIALS>

I want to update the data of both child node(username and password) of MEMBER_BENEFITS and ARTICLE_DOWNLOAD and so on.

Does any one have idea about this?

Upvotes: 0

Views: 1144

Answers (2)

Davide Pastore
Davide Pastore

Reputation: 8738

You could use jsoup.

String xml = "<CREDENTIALS>...</CREDENTIALS>";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());

//MEMBER_BENEFITS
Element memberBenefits = doc.select("MEMBER_BENEFITS").first();
memberBenefits.select("USERNAME").text("newusername");
memberBenefits.select("PASSWORD").text("newpassword");

//ARTICLE_DOWNLOAD
Element articleDownload = doc.select("ARTICLE_DOWNLOAD").first();
articleDownload.select("USERNAME").text("newusername");
articleDownload.select("PASSWORD").text("newpassword");

Upvotes: 1

Tarek
Tarek

Reputation: 771

Use a XML parser to parse this XML into Java and update the XML nodes using simple java commands. After modification, parse back the java program to XML.

You can check following tutorial:

http://examples.javacodegeeks.com/core-java/xml/java-xml-parser-tutorial/

Upvotes: 0

Related Questions