hi,
i m new to xml..in my project..i need to develop xml file..i had done it...i used jsp pages for entering the values in xml file...if i enter the same value which is present in xml file...it should throw an alert that 'Already the name exists'...can anyone help me out..i used DOM for creation of XML file...
Thanks in advance..
madhu.
13 4817
What is the structure of your xml like? Are these values stored in particular elements? Can you get a list of these elements, eg using some sort of getElementsByTagName function? or SelectNodes?
hi,
here is my XML file...i entered all the values using jsp page..i.e P_Name,M_Status..etc...if i re-enter the values..it should show me an alert that the name already exists.. - <PD>
-
<GT>
-
<GL>
-
<P_Name>Hello</P_Name>
-
<M_Status>M</M_Status>
-
<E_Qualification>B.Tech</E_Qualification>
-
</GL>
-
</GT>
-
</PD>
Thanks in Advance,
madhu.
hi,
i created xml file using DOM...if i enter the values...which are present in xml file...it is not showing any error...it is accepting the value...can anyone suggest me how to check the enter value with existing value...i need help from anyone urgently...
Thanks in advance,
madhu.
I would need to see some code to make a sensible statement.
hi,
this is my code waiting for the reply.. - 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("LOGIN");
-
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("LIST");
-
Element subchild = doc.createElement("GENERAL");
-
// adding a node after the last child node of the specified node.
-
node.appendChild(child);
-
child.appendChild(subchild);
-
-
Element child1 = doc.createElement("ID");
-
subchild.appendChild(child1);
-
Text text = doc.createTextNode(id);
-
child1.appendChild(text);
-
-
Element element = doc.createElement("NAME");
-
subchild.appendChild(element);
-
Text text1 = doc.createTextNode(name);
-
element.appendChild(text1);
-
-
Element element1 = doc.createElement("GENDER");
-
subchild.appendChild(element1);
-
Text text2 = doc.createTextNode(gender);
-
element1.appendChild(text2);
-
-
Element element2 = doc.createElement("QUAL");
-
subchild.appendChild(element2);
-
Text text3 = doc.createTextNode(qual);
-
element2.appendChild(text3);
-
-
// 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();
-
-
}
-
}
this is my code..can u chk it nd do me the needful..
Thanks in advance,
madhu.
"...if i enter the same value"
How exactly are you entering these values?
What type of DOM?
It looks like you can just search your P_Name nodes every time you make an entry.
As you add the nodes, create a vector to hold the already used values. When you try to add new values, compare against the values in the vector.
@jkmyoung
hi,
if i enter the 'P_NAME' value as 'Hello' again it is taking ...how to solve the prob.
Thanks,
madhu.
Did you try what we suggested? Please show us the code for your try.
hi,
i had done by using Ajax..it is validating..the problem is if i entering the values it is not taking...can i get any suggestion what is the problem in the code...here is the code...waiting for the reply..
Thanks in advance,
madhu.
-
if(trim(childNode.nodeValue) == trim(id)){
-
alert("already Present");
-
break;
-
}
-
In this code, all you do is break. It doesn't stop the node from being added. I suggest setting a flag in this case; check if the flag is set before you add the node.
@jkmyoung
hi,
i had done...it is getting added..can u suggest me where is the problem...here is the code... - function parseMsg() {
-
var id = document.getElementById("id").value;
-
response = xmlHttp.responseXML.documentElement;
-
var node = response.getElementsByTagName("PACK_NAME");
-
for ( var i = 0; i < node.length; i++) {
-
var node1 = node.item(i);
-
var childNodes = node1.childNodes;
-
for ( var j = 0; j < childNodes.length; j++) {
-
var childNode = childNodes.item(j);
-
if (childNode.nodeType == Node.TEXT_NODE) {
-
//alert(childNode.nodeValue);
-
if (id == childNode.nodeValue) {
-
alert("Pack Already Exists");
-
return true;
-
}
-
}
-
}
-
}
Thanks in advance...
waiting for the reply,
madhu.
@madhuriks
Hi,
I am trying to do something similar, so may have a solution for you.
this is my code, which shows the alert when the entry is found in the xml. -
if (window.XMLHttpRequest)
-
{// code for IE7+, Firefox, Chrome, Opera, Safari
-
xmlhttp=new XMLHttpRequest();
-
}
-
else
-
{// code for IE6, IE5-
-
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
xmlhttp.open("GET","Products.xml",false);
-
xmlhttp.send();
-
xmlDoc=xmlhttp.responseXML;
-
var z=xmlDoc.getElementsByTagName("PRODUCT");
-
-
function CheckProduct(){
-
var checkthis=document.getElementById("ProductNo").value;
-
-
for(i=0; i<z.length; i++){
-
var filevalue=z[i].getElementsByTagName("PRODUCT_NO")[0].childNodes[0].nodeValue;
-
if(checkthis==filevalue){
-
alert("This already exists");
-
}
-
}
-
-
}
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by PeterW |
last post: by
|
2 posts
views
Thread by Pawel Wrobel |
last post: by
|
13 posts
views
Thread by MLH |
last post: by
|
1 post
views
Thread by Dixie |
last post: by
|
8 posts
views
Thread by News Microsoft |
last post: by
|
1 post
views
Thread by Edwina Rothschild |
last post: by
| | |
1 post
views
Thread by Agnes |
last post: by
| | | | | | | | | | | |