I have the following xml file:
<?xml version="1.0"?>
<SMS>
<ALERTS>
<SNO>1</SNO>
<MOBILENUMBER>1234567890</MOBILENUMBER>
<TEXT>HI</TEXT>
<TIME></TIME>
<RESPONSEID></RESPONSEID>
</ALERTS>
</SMS>
I would like to insert string value into XML Tag(RESPONSEID). I have tried with setTextContent and setNodeValue methods which didn't work for me.
Following is my little program:
public void selectRecords() throws SQLException
{
File file = new File("E:\\Workspace\\netbeans-workspace\\DOM_Parser_Sample\\MyXMLFile1.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("ALERTS");
for (int s = 0; s < nodeList.getLength(); s++)
{
Node firstNode = nodeList.item(s);
if (firstNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) firstNode;
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
res = response.append(inputLine).toString();
}
in.close();
NodeList responseList = eElement.getElementsByTagName("RESPONSEID").item(0).getChildNodes();
responseList.item(0).setTextContent(res);
}
}
}
No comments:
Post a Comment