Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read, update and delete the existing XML file using node attribute value in Java

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:

  1. Open the file c://java/Files/12345.xml
  2. Search for all nodes and find the basis of value of n(277). There will be only one record for 277
  3. 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"/>).
  4. Save the updated XML on same location.

Deleting Method Logic:

  1. Open the file c://java/Files/12345.xml
  2. Search for all nodes and find on the basis of value of n(277). There will be only one record for 277.
  3. If this value exists in node attribute "n" then it will delete that node from the xml, else no update required.
  4. Save the updated XML on same location.

Would appreciate if you share some good example or links for above implementaion.

Thanks.

like image 547
Manoj Singh Avatar asked Mar 05 '26 10:03

Manoj Singh


1 Answers

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:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pAction" select="'delete'"/>
 <xsl:param name="pN" select="277"/>
 <xsl:param name="pU" select="'/de/english/plan_book/plan_and_book.aspx'"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="ps">
  <ps>
    <xsl:apply-templates select=
     "*[not($pAction = 'delete')]
     |
      *[$pAction = 'delete' and not(@n = $pN)]
     "/>
    <xsl:if test="$pAction = 'add' and not(p[@n = $pN])">
      <p n="{$pN}" u="{$pU}"/>
    </xsl:if>
  </ps>
 </xsl:template>

 <xsl:template match="p">
  <xsl:choose>
    <xsl:when test="not(@n = $pN)">
      <xsl:call-template name="identity"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:if test="not($pAction = 'delete')">
          <xsl:call-template name="identity"/>
      </xsl:if>
     </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<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>

the wanted, correct result is produced:

<ps>
   <p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
   <p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
</ps>

when the parameter $pAction is changed to:

 <xsl:param name="pAction" select="''add'"/>

then the result of the transformation is the same XML document (unchanged).

When the parameter is:

 <xsl:param name="pAction" select="''add'"/>

and the XML document is:

<ps>
    <p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
    <p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
</ps>

then the result is:

<ps>
   <p n="359" u="/ae/arabic/plan_book/plan_and_book.aspx"/>
   <p n="410" u="/ao/english/plan_book/plan_and_book.aspx"/>
   <p n="277" u="/de/english/plan_book/plan_and_book.aspx"/>
</ps>
like image 132
Dimitre Novatchev Avatar answered Mar 06 '26 22:03

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!