How to set the text content of a node in XML in java?



Consider the example,



<PARAMETER FIELD="QUERYSTORE_TYPE" LABEL="Query Type" NAME="QUERYSTORE" NUM_ROW="40">
<DEFAULT>""</DEFAULT>
</PARAMETER>

<PARAMETER FIELD="GUESS_SCHEMA" LABEL="Guess Schema" NAME="GUESS_SCHEMA" NUM_ROW="40">
<DEFAULT></DEFAULT>
</PARAMETER>

<PARAMETER FIELD="MEMO_SQL" LABEL="Query" NAME="QUERY" NUM_ROW="45" REQUIRED="true">
<DEFAULT>"select id, name from employee"</DEFAULT>
</PARAMETER>


now I have to remove all double quotes in content of DEFAULT tag. I tried doing the following


NodeList nList = doc.getElementsByTagName("PARAMETER");



for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
NodeList nl = nNode.getChildNodes();
for(int k = 0; k<nl.getLength(); k++)
{
if(nl.item(k).getNodeName().equals("DEFAULT"))
{
nl.item(k).setTextContent(nl.item(k).getTextContent().replaceAll("\"", ""));
}
}
}


}


But its not working. When I open the XML file it still contains those double quotes. How do i do this? And also wherever there is double quote, I have to add another attribute to PARAMETER element namely encode with value indicating if there was a double quote in DEFAULT tag. ie



<PARAMETER FIELD="QUERYSTORE_TYPE" LABEL="Query Type" NAME="QUERYSTORE" NUM_ROW="40" encode ="true">
<DEFAULT></DEFAULT>
</PARAMETER>

<PARAMETER FIELD="GUESS_SCHEMA" LABEL="Guess Schema" NAME="GUESS_SCHEMA" NUM_ROW="40" encode = "false">
<DEFAULT></DEFAULT>
</PARAMETER>

<PARAMETER FIELD="MEMO_SQL" LABEL="Query" NAME="QUERY" NUM_ROW="45" REQUIRED="true" encode="true">
<DEFAULT>"select id, name from employee"</DEFAULT>
</PARAMETER>


How do I do this?


No comments:

Post a Comment