aspose file tools*
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
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
Author

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
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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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
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

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
 
 
subject: Help regarding how to check the element entered is equal or not in xml file