I am trying to read/update/delete the XML file on the basis of value found.
I have a XML with name 123456.xml with below format.
<ps>
<p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
<p n="277" u="/ae/english/plan_book/plan_and_book.aspx"/>
<p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
</ps>
Now my new method in java will get Filepath (c://java/Files/12345.xml), n(277 – the value which will be checked in file) and U (“/de/english/plan_book/plan_and_book.aspx”).
Logic for my java method will as below, but really don’t know how to write.
Adding/Appending Method Logic:
- Open the file
c://java/Files/12345.xml - Search for all nodes and find the basis of value of n(277). There will be only one record for 277
- If this value exists in the file then no updates are required else add the new node in xml file, so for example if the value of n would have been (777), as this attribute record does not exists in file then it would have added a new record in file (
<p n="777" u="/ao/english/plan_book/plan_and_book.aspx"/>). - Save the updated XML on same location.
Deleting Method Logic:
- Open the file
c://java/Files/12345.xml - Search for all nodes and find on the basis of value of n(277). There will be only one record for 277.
- If this value exists in node attribute “n” then it will delete that node from the xml, else no update required.
- Save the updated XML on same location.
Would appreciate if you share some good example or links for above implementaion.
Thanks.
This kind of processing is usually easier and simpler (no regEx required) to specify in XSLT than in an imperative language.
The XSLT transformation below can be used directly, or it can give an idea how to implement the same algorithm in another language:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
when the parameter
$pActionis changed to:then the result of the transformation is the same XML document (unchanged).
When the parameter is:
and the XML document is:
then the result is: