0

I will preface the below with stating i am new to Java in general:

I have the need to insert the a value into an already existing XML in the below case for the attribute "directory".

    <?xml version="1.0" encoding="UTF-8"?>
<DTABLEITEM Name="0216.11 neu">
              <DVALUE Name="Type" Value="2"/>
              <DVALUE Name="Workspace" Value="ST_0216.11.ARD"/>
              <DVALUE Name="IntSetupFile" Value=""/>
              <DVALUE Name="IntDocPlotCmd" Value=""/>
              <DVALUE Name="Directory" Value=""/>
              <DVALUE Name="Resource" Value=""/>
/DTABLEITEM>

Below is an example of code that works in one use case where i was able to replace a value easily.

#foreach ($item1 in $xmlf1.find("/."))
$item1.toString().replace("<<wfp.Macro_Settings_Name/>>","(Broken or Missing) <<wfp.Macro_Settings_Name/>>")
#end

The below returns my XML without any modifications but after many hours searching i cannot find anything that works for me either i need a way of replacing that whole line as a string or a way to set the value neither of which i have been successful with.

I understand the below is simply loading the XML and printing

#foreach ($item1 in $xmlf1.find("/."))
$item1
#end
  • 1
    What you want to look into is called "parsing". Velocity has an XML tool (https://velocity.apache.org/tools/2.0/tools-summary.html#XmlTool) and Java has access to various XML parsers. Doing a String replace is relatively naive way and will only work for simple cases. A parser turns the String into a data structure which more accurately reflects the XML structure, which you can then manipulate. For example you can find the node of interest and change its value, then print out the resulting XML document. – Adam Jan 28 '20 at 23:09
  • @Adam i understand i can parse however i do not understand how i can substitute the value. I am running the code through a workflow engine so my parameters are set differently any example i could deconstruct would be much appreciated. – Ryan Carter Jan 29 '20 at 13:17

0 Answers0