Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i had developed xml file using DOM,if i enter the values from front-end i.e., jsp page...an xml file is creating..if i enter the same value in the xml file...it is not showing any error...i used javascript for validation...for eg. my XML file is

<GENERAL>
<NAME>Hello</NAME>
<STATUS>M</STATUS>
<QUAL>B.Tech</QUAL>
</GENERAL>


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

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

Thanks,
madhu.
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
hi,
here with i m sending the code...can you check it..

package login;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class register extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
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");
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
java.io.File file = new java.io.File("E:/login.xml");
Document doc = null;
if (file.exists()) {
doc = docBuilder.parse(file);
} else {
doc = docBuilder.newDocument();
Element root = doc.createElement("GENERAL");
doc.appendChild(root);
}
createXmlTree(doc, id, name, gender, qual);
}
catch (Exception e) {
System.out.println(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.
node.appendChild(child);
child.appendChild(subchild);

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

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

// 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)));
bw.write(xmlString);
bw.flush();
bw.close();

}
}

can you suggest me where to do the modifications..

Thanks in advance,
madhu.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic