I have an XML file (client_23.xml)
as shown below. And I have a String variable out
which I need to insert a particular spot in below XML. This is not my full XML as I have lot of other nested stuff under which function tag will be in and it is not consistent since this XML is getting generated through code so I nee
<?xml version="1.0"?>
<clients>
<!-- some other code here -->
<function>
</function>
<function>
</function>
<function>
<name>data_values</name>
<variables>
<variable>
<name>temp</name>
<type>double</type>
</variable>
</variables>
<block>
<opster>temp = 1</opster>
</block>
</function>
</clients>
I need to parse the above XML and find a function whose name is data_values
and then insert out
string variable in <block>
tag. This is not my full XML as I have lot of other nested stuff under which function tag will be in and it is not consistent since this XML is getting generated through code so I need to parse and iterate and find it and then put it.
So final xml will look like this:
<?xml version="1.0"?>
<clients>
<!-- some other code here -->
<function>
</function>
<function>
</function>
<function>
<name>data_values</name>
<variables>
<variable>
<name>temp</name>
<type>double</type>
</variable>
</variables>
<block>
<!-- new stuff added and old things were gone -->
<opster>hello = world</opster>
<opster>abc = def</opster>
</block>
</function>
</clients>
Below is the code I got but I am not able to understand how can I put out variable inside data_values
function in a block tag.
StringBuilder out = new StringBuilder();
// some data in out variable, properly formatted with new lines.
String location = key.getPathName();
String clientIdPath = location + "/" + "client_23.xml";
File fileName = new File(clientIdPath);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(fileName);
NodeList dataValueFunction = document.getElementsByTagName("function");
// I have started iterating but I am not able to understand how to insert "out"
// variable inside block
for (int i = 0; i < dataValueFunction.getLength(); i++) {
Node node = dataValueFunction.item(i);
System.out.println(node.getNodeName());
NodeList childList = node.getChildNodes();
for (int j = 0; j < childList.getLength(); j++) {
Node node1 = childList.item(j);
if (node1 != null && node1.getNodeName().equalsIgnoreCase("name")
&& node1.getTextContent().equalsIgnoreCase("data_values")) {
// now what should I do here?
}
}
}