This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Help regarding how to check the element entered is equal or not in xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Help regarding how to check the element entered is equal or not in xml file" Watch "Help regarding how to check the element entered is equal or not in xml file" New topic

Help regarding how to check the element entered is equal or not in xml file

madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
i had developed xml file using DOM,if i enter the values from front-end i.e., jsp xml file is creating..if i enter the same value in the xml is not showing any error...i used javascript for validation...for eg. my XML file is


if i enter 'Hello' in the <NAME> tag it is taking...i dont want that to be should throw an error 'Already the Name exists'....

Can anyone help me out...waiting for the reply.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19850

Instead of unconditionally adding the element, instead use an if-statement to see if the string is "Hello", and if it is, then throw an exception?

I put a question mark there because you really don't have a specific question. You have a vague description of a system with numerous components and you want to change one of those components in some way (surely not just to exclude the word "Hello"). A specific question would be far more practical. If you have code which you want to change, then posting that code would be useful too.
madhuri kunchala
Ranch Hand

Joined: Mar 30, 2010
Posts: 350
here with i m sending the code...can you check it..

package login;

import javax.servlet.*;
import javax.servlet.http.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;

public class register extends HttpServlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
PrintWriter pw = response.getWriter();
String id = "";
String name = "";
String gender = "";
String qual = "";
id = request.getParameter("id");
name = request.getParameter("name");
gender = request.getParameter("gender");
qual = request.getParameter("qual");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); file = new"E:/login.xml");
Document doc = null;
if (file.exists()) {
doc = docBuilder.parse(file);
} else {
doc = docBuilder.newDocument();
Element root = doc.createElement("GENERAL");
createXmlTree(doc, id, name, gender, qual);
catch (Exception e) {

private void createXmlTree(Document doc, String id, String name, String gender, String qual)
throws Exception {
Node node = doc.getFirstChild();
Node childnode =doc.getChildNodes().item(1);
Element child = doc.createElement("NAME");

// adding a node after the last child node of the specified node.

Element child1 = doc.createElement("STATUS");
Text text = doc.createTextNode(id);

Element element = doc.createElement("QUAL");
Text text1 = doc.createTextNode(name);

// TransformerFactory instance is used to create Transformer objects.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();

File file = new File("E:/login.xml");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file)));


can you suggest me where to do the modifications..

Thanks in advance,
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
1) UseCodeTags
2) TellTheDetails

You still didn't provide a valid problem description. What is the expected behaviour (in detail) and where does it differ from the encountered behaviour (in detail)? Or simply speaking: whaddayawant?

Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
I agree. Here's the link:
subject: Help regarding how to check the element entered is equal or not in xml file
It's not a secret anymore!