Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

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

Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Posts: 28271
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
crispy bacon. crispy tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic